# Pulling data from an array

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 06-11-2010
lpaulgib
Pulling data from an array
I'm trying to make a small program that gives you the average age of your whole family. I'm into arrays in my book, and I'm trying to experiment with stuff. My thoughts behind the program are this....

1. Get total number of family members.
2. get ages of all the members and insert it into an array.
3. Total up all the ages inside the array and insert that total into a new variable.
4. Send the two variables to a function do the actual averaging and post a witty comment.

Code:

```//This is going to be a practice program that averages the age of your family #include <iostream> using namespace std; /* familyfunct is supposed to take the 2 variables from the main function, divide them into a third variable average. then run a series of if loops dependant on averages number. */ int familyfunct(int combinedages, int famtot, int average){     cout<<"About to do the math.....\n\n\n";     average = (combinedages/famtot);     cout<< average << endl;         if(average>80){             cout<<"You're all old!";         }         if(average< 20){             cout<<"You're a young family!";         }         if(average< 30){             cout<<"You're getting older!";         }         else{             cout<<"You're pretty normal";         } } int main(){     int ages[100];  //this array holds the ages of all family members up to 100 people.     int FamiTotal, AverAge;     int i;  //for for loop         cout<< "How many family members do you have?";     cin>>FamiTotal;     cout<<"Enter each family members age. Press enter when complete.";     cin>>ages[];  //this is supposed to insert each family member into the ages[] array /* This for loop is supposed to extract the data from the array ages[] then add them all up. This is the main problem of the program. */         for(i=0; i<100; i++){         AverAge= ages[i]+ ages[i];     }     familyfunct(AverAge, FamiTotal); // sends AverAge and FamiTotal     return 0; }```

Here is what I came up with. I'm new to arrays, and my book doesn't seem to go too much into them right now, so I'm experimenting.

The bolded area of my code is the area that I need help on. How do I extract the list of numbers from my array and then add them up to put that total into a new variable? I figured it'd be some type of for loop, but I don't really know.

Secondly, I'm getting some compiler errors that seem unrelated to array issue I'm having. Can't quite figure them out, but here they area...

Code:

``` C:\Users\justin\C++\My Code\arrays1.cpp||In function 'int main()':| C:\Users\justin\C++\My Code\arrays1.cpp|43|error: expected primary-expression before ']' token| C:\Users\justin\C++\My Code\arrays1.cpp|10|error: too few arguments to function 'int familyfunct(int, int, int)'| C:\Users\justin\C++\My Code\arrays1.cpp|50|error: at this point in file| ||=== Build finished: 3 errors, 0 warnings ===|```

Well, any help is appreciated! This program is just me experimenting since I'm a self learner. I don't really have anyone to go to but you guys and my pdf.
• 06-11-2010
rags_to_riches
Just like you're using an index to extract the data from the array, you need to use an index to add each individual element to the array.

As far as your other error is concerned,
Code:

`too few arguments to function 'int familyfunct(int, int, int)`
you really can't understand that plain-English error, when the error is specifically pointing you to this line:
Code:

`familyfunct(AverAge, FamiTotal); // sends AverAge and FamiTotal`
• 06-11-2010
iMalc
Code:

`cin>>ages[];  //this is supposed to insert each family member into the ages[] array`
Well it doesn't. What made you think that was valid code?
Are you programming by guessing the syntax?
• 06-11-2010
lpaulgib
Quote:

Originally Posted by iMalc
Code:

`cin>>ages[];  //this is supposed to insert each family member into the ages[] array`
Well it doesn't. What made you think that was valid code?
Are you programming by guessing the syntax?

iMalc, by that comment I meant that each entry is supposed to insert the age into the array. The book hasn't expressly said the syntax, but going off the example programs. it looked to me like the syntax was cin >> array[] . I'm going off Sams Teach Yourself C++ in an Hour a Day. It's been great at explaining things until now.

I understand the concept of an index. When I declared int ages[100], I put 100 as a safe number just because it's a safe number that it would never get that high. But the actual number of elements I'm going to use wouldn't be certain until I get that input into the variable. So how should I go about that?
• 06-11-2010
Programmer_P
Quote:

Originally Posted by lpaulgib
I understand the concept of an index. When I declared int ages[100], I put 100 as a safe number just because it's a safe number that it would never get that high. But the actual number of elements I'm going to use wouldn't be certain until I get that input into the variable. So how should I go about that?

Do:

Code:

```int input = 0; for (int i = 0; i < FamiTotal; i ++) { cin>>input; ages[i] = input; }```
• 06-11-2010
lpaulgib
Thanks Prog, I actually just did that before I checked this post. I cleaned up the code some. This is turning into my first little personal code project. I revised the part for getting all the ages into the array into this...

Code:

``` int i;     for(i=0; i<FamTotal; i++){     cout<<"Enter the age for family member.";     cin>>ages[i];     }```
My only problem right now is how I get however many elements are actually being used and total them up. I'm trying to think of how I should word it, but I'm thinking I should somehow use a for loop to add ages[] to a variable and increment it each time through until I get to the limit set by FamTotal. Then save that total.

My other option would be to literally add up every single element written the long way.

Any thoughts?
• 06-12-2010
iMalc
That's not the only problem.
I hope you can work out what is wrong with this line, now that I'm pointing out to you that it is not right:
Code:

`AverAge= ages[i]+ ages[i];`
• 06-12-2010
Elysia
As an extra task, you may want to look into replacing the array with std::vector instead. That way you don't have to guess the number of family entries.
Plus using the .at member function is all about security, which is a very important topic these days.
• 06-12-2010
Programmer_P
Quote:

Originally Posted by lpaulgib
Thanks Prog, I actually just did that before I checked this post. I cleaned up the code some. This is turning into my first little personal code project. I revised the part for getting all the ages into the array into this...

Code:

``` int i;     for(i=0; i<FamTotal; i++){     cout<<"Enter the age for family member.";     cin>>ages[i];     }```
My only problem right now is how I get however many elements are actually being used and total them up. I'm trying to think of how I should word it, but I'm thinking I should somehow use a for loop to add ages[] to a variable and increment it each time through until I get to the limit set by FamTotal. Then save that total.

My other option would be to literally add up every single element written the long way.

Any thoughts?

That's easy:

Code:

```for (int i = 0; i < FamTotal; i++) { //note that you can declare 'i' inside the for loop   cout<< "Enter the age for family member " << i  << ":" <<endl;   cin>>ages[i]; } int elementsUsed = FamTotal; //though you could also use FamTotal```
The number of elements accessed with values stored in them will be equal to FamTotal for a normal 1-based count.
• 06-12-2010
lpaulgib
Quote:

Originally Posted by Elysia
As an extra task, you may want to look into replacing the array with std::vector instead. That way you don't have to guess the number of family entries.
Plus using the .at member function is all about security, which is a very important topic these days.

I heard about vectors, but the book doesn't address them for another like 6 or 7 chapters. I'm just trying to use what I've learned as of now.

iMalc, I've already recognized that that part of the code is just wrong. It's actually what I'm talking about right now. I'm trying to see how I can add up all the elements in the function (Like the actual ages, not just the number of elements used) and post the result to a variable. I could just go ages[0] + ages[1] + ages[2]... all the way to 99.

Or if someone knows how I could do a loop that would run based off a variable number. (I.E- how many family members the user says he hays).

I'm stuck here.
• 06-12-2010
Programmer_P
Quote:

Originally Posted by lpaulgib
I heard about vectors, but the book doesn't address them for another like 6 or 7 chapters. I'm just trying to use what I've learned as of now.

iMalc, I've already recognized that that part of the code is just wrong. It's actually what I'm talking about right now. I'm trying to see how I can add up all the elements in the function (Like the actual ages, not just the number of elements used) and post the result to a variable. I could just go ages[0] + ages[1] + ages[2]... all the way to 99.

Or if someone knows how I could do a loop that would run based off a variable number. (I.E- how many family members the user says he hays).

I'm stuck here.

Ok, if you want to get the ages added up, do this:

Code:

```int totalAges = 0; for (int i = 0; i < FamTotal; i++) { //note that you can declare 'i' inside the for loop   cout<< "Enter the age for the next family member: " <<endl;   cin>>ages[i];   totalAges += ages[i]; } cout<< "\n\nFamily ages added up is: << totalAges <<endl;```
Here is an example of that working:

Quote:

Enter the age for the next family member: >> 20
Enter the age for the next family member: >> 40

Family ages added up is: 60
• 06-12-2010
lpaulgib
That one line of code did everything I need. Thanks Prog! That's exactly what I had been looking for.
• 06-12-2010
lpaulgib
Here is my final working code. Any suggestions or tips for my style or anything let me know!

Code:

```//This is going to be a practice program that averages the age of your family #include <iostream> using namespace std; /* familyfunct is supposed to take the 2 variables from the main function, divide them into a third variable average. then run a series of if loops dependant on averages number. */ int familyfunct(int combinedages, int famtot){     cout<<"\nAbout to do the math.....\n\n\n";     int average = (combinedages/famtot);     cout<< average << endl << endl;         if(average>80){             cout<<"You're all old!" << endl;         }         if(average< 20){             cout<<"You're a young family!" << endl;         }         if(average< 30){             cout<<"You're getting older!" << endl;         }         else{             cout<<"You're pretty normal" << endl;         } } int main(){     int ages[100] {0};  //this array holds the ages of all family members     int FamTotal;     int i, x;     cout<< "How many family members do you have?";     cin>>FamTotal;     int totalAges=0;     for(i=0; i<FamTotal; i++){     cout<<"Enter the age for family member.";     cin>>ages[i];     totalAges += ages[i];  //this adds up the ages to a total.     }     familyfunct(totalAges, FamTotal); // sends totalAges and FamTotal to funtion familyfunct     return 0; }```

Again, let me know what you think about my code and my coding style.
• 06-12-2010
Programmer_P
Quote:

Originally Posted by lpaulgib
Here is my final working code. Any suggestions or tips for my style or anything let me know!

Code:

```//This is going to be a practice program that averages the age of your family #include <iostream> using namespace std; /* familyfunct is supposed to take the 2 variables from the main function, divide them into a third variable average. then run a series of if loops dependant on averages number. */ int familyfunct(int combinedages, int famtot){     cout<<"\nAbout to do the math.....\n\n\n";     int average = (combinedages/famtot);     cout<< average << endl << endl;         if(average>80){             cout<<"You're all old!" << endl;         }         if(average< 20){             cout<<"You're a young family!" << endl;         }         if(average< 30){             cout<<"You're getting older!" << endl;         }         else{             cout<<"You're pretty normal" << endl;         } } int main(){     int ages[100] {0};  //this array holds the ages of all family members     int FamTotal;     int i, x;     cout<< "How many family members do you have?";     cin>>FamTotal;     int totalAges=0;     for(i=0; i<FamTotal; i++){     cout<<"Enter the age for family member.";     cin>>ages[i];     totalAges += ages[i];  //this adds up the ages to a total.     }     familyfunct(totalAges, FamTotal); // sends totalAges and FamTotal to funtion familyfunct     return 0; }```

Again, let me know what you think about my code and my coding style.

I think you should remove the line that says "int i, x;" completely (especially since 'x' is an unused variable), and change the for loop to declare i. Like this:

Code:

```int totalAges=0; for(int i=0; i<FamTotal; i++){     cout<<"Enter the age for next family member:";     cin>>ages[i];     totalAges += ages[i];  //this adds up the ages to a total. }```
• 06-12-2010
lpaulgib
Is there a reason why I should declare the integer in the actual loop rather than outside the loop in the beginning? I thought it would be cleaner code to have all the integers declared in the beginning. Is there a functional reason to do it inside the loop? I had forgotten to take the x variable out just because it was being used, then i cleaned out that loop...

And also my indention and tabbing. Does it look clean? So far the only people that normally read the code i write is myself. And of course it looks good to me. Do you see any potential problems?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last