3 Replies - 312 Views - Last Post: 02 September 2013 - 04:37 PM Rate Topic: -----

#1 wjg284  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-August 13

counting int array entities that are the same

Posted 02 September 2013 - 10:52 AM

In the display function, I seem to get a lot of garbage returned to me along with the expected output
#include<iostream>
using namespace std;

void ages(int[], int, int, int);
void display(int[], int);


//************************************************************************
//                        MAIN                                          //
//************************************************************************

int main(){
    
    int size=50;
    int getAge[size];
    int age=0;
    int count=0;
    
    cout<<"Input an age, 0 to 100, -99 to stop. \n";
    ages(getAge,size,count,age);
    display(getAge,size);
    
system("pause");
return 0;
}

//************************************************************************
//                   AGES                                               //
//************************************************************************

void ages(int getAge[], int size, int count, int age){

     cin>>age;
     
     while(age != -99 && count < size){
          count++;
          getAge[count-1]=age;
          cout<<"Input an age, 0 to 100, -99 to stop. \n";
          cin>>age;
          }
     }
          
//************************************************************************
//                   DISPLAY                                            //
//************************************************************************                
     
void display(int getAge[],int size){
     

     int temp = 0;
     for(int count=0; count < size ; count++){
             
          if(getAge[count] > 0){
               temp=0;  
               for(int index=0; index<size; index++){
                    if(count+1 == getAge[index]) 
                         temp+=1;
               }
          if(temp != 0)
          cout<<"\nThe total number of people " << count + 1 << " years is old " << temp <<endl;
          }   
     }
}



Is This A Good Question/Topic? 0
  • +

Replies To: counting int array entities that are the same

#2 samuraitux  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 65
  • Joined: 29-April 09

Re: counting int array entities that are the same

Posted 02 September 2013 - 10:57 AM

View Postwjg284, on 02 September 2013 - 12:52 PM, said:

In the display function, I seem to get a lot of garbage returned to me along with the expected output
#include<iostream>
using namespace std;

void ages(int[], int, int, int);
void display(int[], int);


//************************************************************************
//                        MAIN                                          //
//************************************************************************

int main(){
    
    int size=50;
    int getAge[size];
    int age=0;
    int count=0;
    
    cout<<"Input an age, 0 to 100, -99 to stop. \n";
    ages(getAge,size,count,age);
    display(getAge,size);
    
system("pause");
return 0;
}

//************************************************************************
//                   AGES                                               //
//************************************************************************

void ages(int getAge[], int size, int count, int age){

     cin>>age;
     
     while(age != -99 && count < size){
          count++;
          getAge[count-1]=age;
          cout<<"Input an age, 0 to 100, -99 to stop. \n";
          cin>>age;
          }
     }
          
//************************************************************************
//                   DISPLAY                                            //
//************************************************************************                
     
void display(int getAge[],int size){
     

     int temp = 0;
     for(int count=0; count < size ; count++){
             
          if(getAge[count] > 0){
               temp=0;  
               for(int index=0; index<size; index++){
                    if(count+1 == getAge[index]) 
                         temp+=1;
               }
          if(temp != 0)
          cout<<"\nThe total number of people " << count + 1 << " years is old " << temp <<endl;
          }   
     }
}



This sounds exactly like what was happening to me. What fixed this for me was initializing all of my Arrays. Try this and see if it helps.
 int getAge[size] = {0};



One of the senior memebers helped me with a problem a few minutes ago and that was what helped me.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: counting int array entities that are the same

Posted 02 September 2013 - 11:06 AM

You should probably return the count value from your ages() function. Then use this value in your other functions to only access elements of the array you actually entered values for.

Also it doesn't make sense to pass both the age and count into that function. You're passing these variables by value, meaning any changes made to them in the function are lost when the function returns.

You may want to study the function tutorials in my signature, they will probably help you better understand how to write and use functions.


Jim
Was This Post Helpful? 0
  • +
  • -

#4 wjg284  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 25-August 13

Re: counting int array entities that are the same

Posted 02 September 2013 - 04:37 PM

much easier way to complete this tack. However I'm still curious to know how to finish the display function. My guess would be a couple for loops with an accumulator and counter. And yes, some variables that are being passed along are senseless; the whole display function, really.



#include<iostream>
using namespace std;


const int TOTALYEARS = 100;                                                     //array will be 0-99

int main(){
    
    int ageFrequency[TOTALYEARS];                                               //declare array 
    int currentAge;
    
    for(int pos=0; pos<TOTALYEARS; pos++)                                       //this loop initallizes all subscripts to 0
         {
         ageFrequency[pos] = 0;
         }
         
    cout<<"Plese input an age from one"                                         //output for primer
        <<" to 100. input -99 to stop "<<endl;
        
    cin>>currentAge;                                                            //input for primer
    
    while(currentAge!=-99)                                                      //while not the sentinal -99
    {
         ageFrequency[currentAge-1] =                                           //if the inputted age is 4, it gets saved in the index of 3
         ageFrequency[currentAge-1]+1;                                          //increments that index by 1
         
         cout<<"Plese input an age from one"                                    //update output
         <<" to 100. input -99 to stop "<<endl;
        
         cin>>currentAge;                                                       //update
         }
         
    for(int ageCounter=0; ageCounter < TOTALYEARS; ageCounter++){               //loop to display all subscripts of array that are not less than zero
         if (ageFrequency[ageCounter] > 0)
              cout<<"The number of people "<<ageCounter + 1<<" years old is "
              <<ageFrequency[ageCounter]<<endl;
              }
              
system("pause");
return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1