How come my counter isnt working?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cozmo11214
    New Member
    • Nov 2011
    • 1

    #1

    How come my counter isnt working?

    I need to print my counters in the main program and not in the function but I cant figure out why I the main program prints 0's and when I test it in the function the counters are correct.

    I have posted my code and output below. The parts i'm having trouble with are bold and underlined. Thanks



    Code:
    //assignment #5
    #include<iostream>
    #include<fstream>
    using namespace std;
    ifstream infile("data.dat");
    const int VALUE=50;
    void readdata(int, int[], int[]);
    void printarray(int, int[]);
    int smallest(int, int[]);
    void construct(int , int [], int [], int []);
    int whatshigher(int, int [], int [], int , int , int );
    int main()
    {
    	int n, small, small_1, small_diff, score1[VALUE], score2[VALUE];
        int difference[VALUE], score1_count=0, score2_count=0, equal_count=0;
        infile>>n;
    	
        readdata(n, score1, score2);
    	
    	cout<<"Score 1 array is printing"<<endl;
    	printarray(n, score1);
    	cout<<"Score 2 array is printing"<<endl;
        printarray(n, score2);
        
        small=smallest(n, score1);
    	cout<<"smallest in array 1"<<endl;
        cout<<small<<endl;
        
        small_1=smallest(n, score2);
    	cout<<"smallest in array 2"<<endl;
        cout<<small_1<<endl;
        
        construct(n, score1, score2, difference);
        cout<<"The smallest in difference: ";
        small_diff=smallest(n, difference);
        cout<<small_diff<<endl;
        
        whatshigher(n, score1, score2, score1_count, score2_count, equal_count); 
        [b][u]cout<<"Array score1 was larger "<<score1_count<<"times"<<endl;
        cout<<"Array score2 was larger "<<score2_count<<"times"<<endl;
        cout<<"The two arrays were equal"<<equal_count<<"times"<<endl;[/u][/b]
        
        
        
        
        
        
        
        
        infile.close();
    	
    system("pause");
    return 0;
    }
    
    //readdata()
    void readdata(int n, int nums1[], int nums2[]){
    
    for(int i=0; i<n; i++){
         infile>>nums1[i];
         infile>>nums2[i];
    }
    
    
    }
    
    
    
    
    
    //printarray()
    void printarray(int lim, int arr[]){
    for(int i=0; i<lim; i++)
    cout<<arr[i]<<" ";
    
    cout<<endl;
    
    
    
    }
         
    
    
    //smallest()
    int smallest(int n, int nums[]){
    //Process array
    int min ;
    for(int i=0; i<n; i++)
    { int num = nums[i];
    if(num < min)
    min = num;
    }
    
    return min;
    }
    
    
    //construct()
    void construct(int k, int old1[], int old2[], int diff[]){
        
        cout<<"The difference array: ";
        for(int i=0; i<k; i++){
             diff[i]=old1[i]-old2[i];
             cout<<diff[i]<<" ";
             }
        cout<<endl;
    }
    
    
    //whatshigher()
    int whatshigher(int n, int score1[], int score2[], int c1, int c2, int c3){
         
         
         
         for(int i=0; i<n; i++){
              if(score1[i]>score2[i]){
                   cout<<"In position "<<i<<", score1 is larger:"
                       <<score1[i]<<" is greater than "<<score2[i]<<endl;
                   c1++;
              }
              else if(score2[i]>score1[i]){
                   c2++;
                   cout<<"In position "<<i<<", score2 is larger:"
                       <<score2[i]<<" is greater than "<<score1[i]<<endl;
                   
                   }
              else if(score1[i]==score2[i]){
                   cout<<"In position "<<i<<", the two arrays have the same value: "
                       <<score1[i]<<endl;
                   c3++;
                   }
         
         }
         
         return c1, c2, c3;
         
    }

    Here is my output:

    Score 1 array is printing
    12 1 51
    Score 2 array is printing
    45 15 60
    smallest in array 1
    1
    smallest in array 2
    15
    The difference array: -33 -14 -9
    The smallest in difference: -33
    In position 0, score2 is larger:45 is greater than 12
    In position 1, score2 is larger:15 is greater than 1
    In position 2, score2 is larger:60 is greater than 51
    Array score1 was larger 0times
    Array score2 was larger 0times
    The two arrays were equal 0times

    Press any key to continue . . .
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Thie code:
    Code:
    return c1, c2, c3;
    isn't doing what you think.

    You can return only one value.

    The comma operator executes the code between the commas. The result of each comma step is discarded. So, c1, ->get c1 do nothing, discard
    Then c2, -> get c2 do nothing then discard. Then c3-> get c3 and return it.

    If you need to retreive multiple values from a function you need output arguments. That is, pass in the address of the variables and have the function change the variables using the address. That is, use pointer arguments.

    Comment

    Working...