Pulling data from an array

This is a discussion on Pulling data from an array within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a small program that gives you the average age of your whole family. I'm into arrays ...

  1. #1
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65

    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.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    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

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    Quote Originally Posted by iMalc View Post
    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?

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by lpaulgib View Post
    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;
    }

  6. #6
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    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?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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];
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by lpaulgib View Post
    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.

  10. #10
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    Quote Originally Posted by Elysia View Post
    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.

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by lpaulgib View Post
    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:

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

    Family ages added up is: 60
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #12
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    That one line of code did everything I need. Thanks Prog! That's exactly what I had been looking for.

  13. #13
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    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.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by lpaulgib View Post
    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.
    }
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    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?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 05:24 PM
  2. Pick data from file store in array
    By swgh in forum C Programming
    Replies: 1
    Last Post: 07-10-2009, 10:57 AM
  3. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  4. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 05:29 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21