questions I got from Herbert Schildt's book: C++ beginners' guide

Write your question here.
Write a program that averages the absolute value of five values entered by the user. I cannot figure out why we need to write avg=0.0 and why avg=avg+abs(val)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
    int i;
    double avg,val;

    avg=0.0;

    for(i=0;i<5;i=i++){
        cout<<"Enter a value:";
        cin>>val;

        avg=avg+abs(val);
    }
    avg=avg/5;
    cout<<"Average of absolute values:"<<avg;

    return 0;
    }
You could also start with arg equal to the first value and add the other four, or build the average in some other way. 0 + val1 + val2 + val3 + val4 + val5 is just one of the several possible ways.

Also, did he really write "i=i++"? that renders the program undefined.

sorry, it's my typo, it should be i++.



which part of the codes corresponds to 0 + val1 + val2 + val3 + val4 + val5?

also, why we write avg=0.0 and then avg=avg/5 later.
Cubbi, could you give me a detail explanantion to the code from line 11 to line 19?
BTW, you should try to get your money back for that book. Or burn it.

http://www.cplusplus.com/forum/lounge/75040/

Try to find another resource for learning C++.
the reason why you set avg = 0.0 is to ensure that the variable doesnt hold any garbage. when you declare a variable, it will hold the value of whatever is located at that specific point in memory if you dont overwrite it. So lets say when average was declared, its value was -234262 in memory, if you were to add to avg, you would get an incorrect answer.

For why avg = avg + abs(val), whats happening is you are getting the sum of the inputted numbers. So lets say avg = 0, then we input 2, then avg = 0 + abs(2) which is now avg= 2, and the process repeats for the duration of the for loop. However, i dont understand why abs() is put there, as taking an average is sign sensitive, unless the book specified that it wanted only positive numbers.

Finally, avg= avg/5 is essentially taking your average. after adding up the five numbers in the for loop, you divide it by 5. I personally believe a variable called sum should have been created to store the sum of the five numbers, rather than store it inside avg, to make it much more apparent whats going on.
...i dont understand why abs() is put there...


Write a program that averages the absolute value of...


Couldn't help but point that out.
avg is used within the for loop as a running sum and only becomes the average when it is finally divided by 5 on line 19. You set avg = 0.0 so the sum starts at zero. The for loop is executed 5 times. Each time through the loop the following happens:

line 14: cout<<"Enter a value:"; The user is prompted to "Enter a value:"

line 15: cin>>val; After the user enters a number and hits the Enter key, the number is stored in the variable val.

line 17: avg=avg+abs(val); The equal symbol is not the same as the equal symbol as used in a mathematical equation, but is called the assignment operator. Think of it as "Evaluate the expression on the right hand side of the equal sign and store (or assign) this value to the variable on the left hand side of the equal sign."

line 18: Marks the end of the for loop at which juncture the variable i is incremented and the condition i<5 is checked and if true the loop is executed again. Finally when i=5 the condition will be false and execution will continue at the next statement following the for loop.

line 19: avg=avg/5; This divides the running sum avg by 5 and stores it back in avg.

Let's trace the for loop with some specific values entered by the user. I'll use the same names Cubbi used above for those values.

val1=3.0, val2=-4.0, val3=1.0, val4=7.0, val5= -2.0

avg=0.0
i=0
val = val1 = 3.0
Evaluate avg+abs(val) which is 0.0 + 3.0 =3.0 and store this in avg so avg=3.0

i=1
val = val2 = -4.0
Evaluate avg+abs(val) which is 3.0 + 4.0 =7.0 and store this in avg so avg=7.0

i=2
val = val3 = 1.0
Evaluate avg+abs(val) which is 7.0 + 1.0 =8.0 and store this in avg so avg=8.0

i=3
val = val4 = 7.0
Evaluate avg+abs(val) which is 8.0 + 7.0 =15.0 and store this in avg so avg=15.0

i=4
val = val5 = -2.0
Evaluate avg+abs(val) which is 15.0 + 2.0 =17.0 and store this in avg so avg=17.0

i=5 and the loop ends.

line19: divide avg by 5 and store the result in avg so avg=17.0/5 = 3.4


A few additional comments about the code:

1. The practice of defining all variables at the beginning of a function has the mark of an old C programmer. C++ programmers generally prefer to define variables as close as possible to where they are first used.
Consider line 8 int i; The variable i is not used except in the for loop so why not define it there? You can eliminate line 8 and change the first line of the for loop to:

for(int i=0;i<5;i++)

2. Line 9 double avg,val; is certainly legal code, however most programmers generally give each variable declaration its own line. Variable names that are strung across the line horizontally are harder to read than if each is on its own line. In addition, it usually a good idea to initialize variables when they are declared. Line 11 which sets avg to zero can actually be moved up to the line where the variable is declared. I would change lines 9-11 to:

1
2
double avg=0.0;
double val=0.0;


The final result:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    double avg=0.0;
    double val=0.0;

    for(int i=0;i<5;i++){
        cout<<"Enter a value:";
        cin>>val;

        avg=avg+abs(val);
    }
    avg=avg/5;
    cout<<"Average of absolute values:"<<avg;

    return 0;
 }

Thank for all your help. Particularly, thanks for your specific explanation, Alrededor.

"2. Line 9 double avg,val; is certainly legal code, however most programmers generally give each variable declaration its own line. Variable names that are strung across the line horizontally are harder to read than if each is on its own line. In addition, it usually a good idea to initialize variables when they are declared. Line 11 which sets avg to zero can actually be moved up to the line where the variable is declared. I would change lines 9-11 to:"-with regards to your this comment, because I am a beginner and started learning C++ programming last week. I thought that I need to follow everything I read from the book before I have finish the beginners' part.
/Off
The Schildt books are not very well accepted, for example, this

http://www.seebs.net/c/c_tcn4e.html

Consider the idea of using multiple sources while learning the language.


Last edited on
TheGrayWolf, would you like to recommond me some good sources of c++ programming for beginners?
Yeah, sure. IMO, this is a good tutorial if you are just starting out

http://www.tutorialspoint.com/cplusplus/

When you are done with these, you may look into Eckel's free e-books that touch upon OOP in-depth

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

All these materials cover the old C++98/03 standard (minor differences), so eventually you'll have to look up some books on C++11. Most of the well known titles (Effective C++, Exceptional C++, etc.) will probably be updated for C++11, although you may read the old editions as well, it's all good stuff, so you can't go wrong. For more info on C++11 titles, take a look at this topic

http://www.cplusplus.com/forum/beginner/109037/

and keep in mind the following list of books

http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

Happy programming!

P.S. ROFL, Duoas, yeah, i totally didn't check if that nickname is available. Oh well, here goes the wolf pack ;D
Last edited on
closed account (z05DSL3A)
OT: How many wolves are on the forum?
Holy crap!




It was really late for me when I posted last... But still I can't believe I missed that...



[edit]
How many wolves are on the forum?

I count six.

    Grey Wolf  
    TheGrayWolf (not the youngest)
    metl wolf http://www.cplusplus.com/user/metl_wolf/
    arctic wolf http://v2.cplusplus.com/user/oi2TURfi/ (actual Wolf)
    Bad Wolf Virus http://www.cplusplus.com/user/Bad_Wolf_Virus/
    Wolf http://www.cplusplus.com/user/Wolf/ (as old as Grey Wolf
Last edited on
Topic archived. No new replies allowed.