incorrect print array help

This is a discussion on incorrect print array help within the C++ Programming forums, part of the General Programming Boards category; i have an input file that im writing an an array then printing out that array to a file but ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    incorrect print array help

    i have an input file that im writing an an array then printing out that array to a file but for some reason its printing out more than once. also the format for my functions such as findMin is coming out incorrectly. please help!

    Code:
    
    #include
    
    
    <iostream>
    
    #include
    <fstream>
    
    #include
    <iomanip>
    
    using
    namespace std;
    
    
    void
     readData(ifstream &inputFile, int StudID[],float Scores[],int& size);
    
    void
     printData(ofstream &outputFile, int StudID[],float Scores[], int& size);
    
    void
     FindMin ( int StudID[], int& size);
    
    void
     FindMax  (int StudID[], int& size);
    
    void
     ComputeAvg(float  Scores[], int& size);
    
    void
     BubbleSort (float Scores [ ], int& size);
    
    
     
    
    //***************************************main fucntion*****************************************************
    
    int
      main()
    
    {
    
                                                               
    //Declare Program variables and Arrays
    
        
    int size = 0;
    
        
    int StudID[50];
    
        
    float Scores[50];
    
    //    char Name[15];
    
        ifstream inputFile;
    
        ofstream outputFile;
    
                                                               
    // open the input and output files
    
        outputFile.open (
    "practice.txt");
    
        inputFile.open (
    "data1");
    
                                                               
    // set the formatting codes for output
    
        outputFile.setf(ios::fixed);
    
        outputFile.precision(2);
    
    
        
    //outputFile.setf(ios::fixed);
    
        
    //outputFile.precision(2);
    
                                                              
    // prints the column titles to the file
    
        outputFile << 
    " The original list of test scores is: \n";
    
        outputFile << 
    "Student ID #    Test Scores \n";
    
                                                          
    // runs the fucntions, prints ids and test scores
    
        
    
        readData( inputFile,  StudID, Scores, size);
    
        printData (outputFile,  StudID, Scores,  size);
    
        FindMin (   StudID, size);
    
        ComputeAvg( Scores, size);
    
        FindMax  ( StudID, size);
    
        BubbleSort ( Scores, size);
    
        
    //ofstream outputFile;
    
        
    //outputFile.open("practice.txt");
    
        
    
                                                            
    // Prints the required texts to the output file
    
        
    
        outputFile << 
    " the lowest test score was " ; //FindMin << " and was acheived by student #" << StudID<< endl;
    
        outputFile << 
    " the highest test score was " ;// FindMax << " and was acheived by student #" << StudID<< endl;
    
        outputFile << 
    " The average score of the group was ";// << ComputeAvg<< endl;
    
        outputFile << BubbleSort<< endl;
    
        inputFile.close();
    
        outputFile.close();
     
    return 0;
    
    }
    
    
    //***************************************************************************************************************
    
    void
     readData(ifstream &inputFile, int StudID[],float Scores[],int& size)
    
    {
     
    // GIVEN:   The Data file for the Students Ids and Test Scores
     
    // TASK:    Read the Scores and Ids from the file into an empty array
     
    // RETURNS: The Student Ids from the File and Their test Scores
     
    //reads in the students IDs and test scores
    
        
    for (int i = 0;i<50; i++) 
    
        {
    
            inputFile>> StudID[i] ;    
      
    // sentinel
     
    if(StudID[i]<0)
     
    return;
                                                  
    // Reads in the test scores
    
            inputFile>> Scores[i];
    
            size++;
    
        }
    
    }
    
    
    //*************************************************************************************************************
    
    void
     printData(ofstream &outputFile, int StudID[],float Scores[], int& size)
    
    {
    
        
    // GIVEN:   The output file, the list of students with their 
    
        
    //          Test Scores, and The students Ids
    
        
    // TASK:    Print out the list of students Ids and Test Scores
    
        
    // RETURNS: Nothing
    
                                                          
    // prints the arrays to the file
    
        
    for (int i = 0;i<50; i++) 
    
        
    for (int i = 0;i<size; i++)
    
        {
    
            outputFile <<
    "   " << StudID[i] << "             " << Scores[i];
    
            outputFile << endl;
    
        }
    
            
    return ;
    
    }
    
    //*****************************************************************************************************************
    
    void
     FindMin ( int StudID[], int& size)
    
         {
    
        
    // GIVEN:   The students Ids and test scores
    
        
    // TASK:    Compute the lowest quiz score
    
        
    // RETURNS: The lowest quiz score
    
        
    int i, MinLoc=0 ;
    
        
    int MinValu = 110;
                                                     
    //finds the lowest test score
     
    for ( i = 0; i < size; i++)
    
             
    if (StudID[i]< MinValu  )
    
             {
    
                     MinValu = StudID[i];
    
                     MinLoc = i;
    
             }
    
                
    return ;
    
          }    
    
          
    
    //****************************************************************************************************
    
    void
     FindMax  (int StudID[], int& size)
    
       {
    
             
    // GIVEN:   The arrays of students ids and test scores
    
             
    // TASK:    Compute the highest test score
    
             
    // RETURNS: The highest test score and the Student Id
    
        
    int i, MaxLoc=0 ;
     
    int MaxValu = -10;
                                                        
    // finds the student with highest test score
     
    for ( i = 0; i < size; i++)
          
    if (MaxValu > StudID[i])
    
                {
    
                   MaxValu = StudID[i];
    
                   MaxLoc = i;
    
                }
    
    return ;
    
        }
    
    //******************************************************************************************************
    
    void
     
        ComputeAvg(float  Scores[], int& size)
    {
          
    // GIVEN:   The arrays of the Student Ids and test Scores
     
    // TASK:    Compute the classes average test score
     
     
    // RETURNS: The classes average test score
     
    int i, total = 0;
     
    int average;
                                                        
    //adds up all the scores and finds the average
         
    for ( i = 0; i < size; i++)    
    
               {
    
                   total += Scores[i];         
                   average = total/size;
    
               }           
    return  ;
    
     }
     
    
    //*********************************************************************************************************
    
    void
     
     BubbleSort (float Scores [], int& size)     // Sorts list  LOW to HIGH 
    
    {
     
    int M, N, temp;
        
    for ( M = 0 ; M < size-1; M++)            // M controls how many times we pass thru the array
         {   
    for ( N = size-1; N > M ; N--)         // N controls at which end of the array we begin and which 
           
    if (Scores[N] < Scores[N-1] )                //  elements are compared and swapped if needed.
    
                {
    
                 temp = Scores[N];
    
                 Scores[N] = Scores[N-1];
    
                Scores[N-1] = temp;
    
                }
    
          }
     
     
    return;  
    
     }     
    


  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    The two loops you have in your code explain the double printing:
    Code:
    for (int i = 0;i<50; i++) 
    {
       for (int i = 0;i<size; i++)
       {
            outputFile << "   " << StudID[i] << "             " << Scores[i];
    
            outputFile << endl;
       }
    }
    I tried to make it readable so that you see what is happening. Remove the loop whose condition depends on a magic number.

    Normally when you call a function like FindMin() it is supposed to return the minimal value to the calling function so that you can store it or print it, but none of your functions, besides main(), have a return value, so there is no place to put the results.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    The two loops you have in your code explain the double printing:
    Code:
    for (int i = 0;i<50; i++) 
    {
       for (int i = 0;i<size; i++)
       {
            outputFile << "   " << StudID[i] << "             " << Scores[i];
    
            outputFile << endl;
       }
    }
    I tried to make it readable so that you see what is happening. Remove the loop whose condition depends on a magic number.

    Normally when you call a function like FindMin() it is supposed to return the minimal value to the calling function so that you can store it or print it, but none of your functions, besides main(), have a return value, so there is no place to put the results.

    sorry im new to coding. how would i go about returning the minimal value to store/print it.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Quote Originally Posted by stlninja7 View Post
    sorry im new to coding. how would i go about returning the minimal value to store/print it.
    When you declare a function like this:
    Code:
    void
     FindMin ( int StudID[], int& size);
    The void stands for the return type, the type of data the function will return. void in particular means that the function returns nothing, but if you use a return value of some type (say int instead), you can do this:
    Code:
    int minimal = FindMin(StudID, size);
    
    cout << " The minimum is " << minimal << endl;

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    ok so your being a huge help! so this is what i got now..
    Code:
    
     
    
    #include
    <iostream>
    
    #include
    <fstream>
    
    #include
    <iomanip>
    
    using
    namespace std;
    
    
    void
     readData(ifstream &inputFile, int StudID[],float Scores[],int& size);
    
    void
     printData(ofstream &outputFile, int StudID[],float Scores[], int& size);
    
    int
     FindMin ( int StudID[], int& size);
    
    int
     FindMax  (int StudID[], int& size);
    
    float
     ComputeAvg(float  Scores[], int& size);
    
    float
     BubbleSort (float Scores [ ], int& size);
    
    
     
    
    //***************************************main fucntion*****************************************************
    
    int
      main()
    
    {
    
    	                                                       
    //Declare Program variables and Arrays
    
    	
    int size = 0;
    
    	
    int StudID[50];
    
    	
    float Scores[50];
    
    //	char Name[15];
    
    	ifstream inputFile;
    
    	ofstream outputFile;
    
    	                                                       
    // open the input and output files
    
    	outputFile.open (
    "practice.txt");
    
    	inputFile.open (
    "data1");
    
    														   
    // set the formatting codes for output
    
    	outputFile.setf(ios::fixed);
    
    	outputFile.precision(2);
    
    
    	
    //outputFile.setf(ios::fixed);
    
    	
    //outputFile.precision(2);
    
    	                                                      
    // prints the column titles to the file
    
    	outputFile << 
    " The original list of test scores is: \n";
    
    	outputFile << 
    "Student ID #    Test Scores \n";
    
    	                                                  
    // runs the fucntions, prints ids and test scores
    
    	
    
    	readData( inputFile,  StudID, Scores, size);
    
    	printData (outputFile,  StudID, Scores,  size);
    
    	FindMin (   StudID, size);
    
        ComputeAvg( Scores, size);
    
        FindMax  ( StudID, size);
    
        BubbleSort ( Scores, size);
    
    	
    //ofstream outputFile;
    
    	
    //outputFile.open("practice.txt");
    
    	
    
    	                                                    
    // Prints the required texts to the output file
    
    	
    
    	outputFile << 
    " the lowest test score was "  << FindMin << " and was acheived by student #" << StudID<< endl;
    
    	outputFile << 
    " the highest test score was " << FindMax << " and was acheived by student #" << StudID<< endl;
    
    	outputFile << 
    " The average score of the group was " << ComputeAvg<< endl;
    
    	outputFile << BubbleSort<< endl;
    
    
    	inputFile.close();
    
    	outputFile.close();
    
    	
    
    	
    return 0;
    
    	
    
    }
    
    
    //***************************************************************************************************************
    
    void
     readData(ifstream &inputFile, int StudID[],float Scores[],int& size)
    
    {
    
    	
    // GIVEN:   The Data file for the Students Ids and Test Scores
    
    	
    // TASK:    Read the Scores and Ids from the file into an empty array
    
    	
    // RETURNS: The Student Ids from the File and Their test Scores
    
    	                                                  
    //reads in the students IDs and test scores
    
    	
    for (int i = 0;i<50; i++) 
    
    	{
    
    		inputFile>> StudID[i] ;	
    
    													  
    // sentinel
    
    if(StudID[i]<0)
    
    
    return;
    
    		                                              
    // Reads in the test scores
    
    		inputFile>> Scores[i];
    
            size++;
    
    	}
    
    }
    
    
    //*************************************************************************************************************
    
    void
     printData(ofstream &outputFile, int StudID[],float Scores[], int& size)
    
    {
    
    	
    // GIVEN:   The output file, the list of students with their 
    
    	
    //          Test Scores, and The students Ids
    
    	
    // TASK:    Print out the list of students Ids and Test Scores
    
    	
    // RETURNS: Nothing
    
    	                                                  
    // prints the arrays to the file
    
    	
    
    	
    for (int i = 0;i<size; i++)
    
    	{
    
    		outputFile <<
    "   " << StudID[i] << "             " << Scores[i];
    
    		outputFile << endl;
    
    	}
    
            
    return ;
    
    }
    
    //*****************************************************************************************************************
    
    int
     FindMin  ( int StudID[], int& size)
    
    	 {
    
    	
    // GIVEN:   The students Ids and test scores
    
    	
    // TASK:    Compute the lowest quiz score
    
    	
    // RETURNS: The lowest quiz score
    
    	
    int i, MinLoc=0 ;
    
        
    int MinValu = 110;
    
    	                                                    
    //finds the lowest test score
    
    	
    for ( i = 0; i < size; i++)
    
             
    if (StudID[i]< MinValu  )
    
             {
    
    				 MinValu = StudID[i];
    
    				 MinLoc = i;
    
    		 }
    
                
    return MinLoc;
    
    	  }	
    
    	  
    
    //****************************************************************************************************
    
    int
     FindMax  (int StudID[], int& size)
    
       {
    
    	     
    // GIVEN:   The arrays of students ids and test scores
    
    	     
    // TASK:    Compute the highest test score
    
    	     
    // RETURNS: The highest test score and the Student Id
    
    	
    int i, MaxLoc=0 ;
    
    	
    int MaxValu = -10;
    
    	                                                    
    // finds the student with highest test score
    
    	
    for ( i = 0; i < size; i++)
    
    	      
    if (MaxValu > StudID[i])
    
    		    {
    
    			   MaxValu = StudID[i];
    
    			   MaxLoc = i;
    
    			}
    
    
    return MaxLoc;
    
    	}
    
    //******************************************************************************************************
    
    
    float
    	ComputeAvg(float  Scores[], int& size)
    
    {
    
    	      
    // GIVEN:   The arrays of the Student Ids and test Scores
    
    	      
    // TASK:    Compute the classes average test score
    
    	      
    // RETURNS: The classes average test score
    
    int i, total = 0;
    
    int average;
    
    	                                                    
    //adds up all the scores and finds the average
    
    for ( i = 0; i < size; i++)	
    
    		   {
    
    			   total += Scores[i];         
    
                   average = total/size;
    
    	       }   
    
                
    return  total;
    
     }
    
    
     
    
    //*********************************************************************************************************
    
    
    float
     BubbleSort (float Scores [], int& size)     // Sorts list  LOW to HIGH 
    
    {
    
        
    int M, N, temp;
    
        
    for ( M = 0 ; M < size-1; M++)            // M controls how many times we pass thru the array
    
         {   
    for ( N = size-1; N > M ; N--)         // N controls at which end of the array we begin and which 
    
                
    if (Scores[N] < Scores[N-1] )                //  elements are compared and swapped if needed.
    
                {
    
                 temp = Scores[N];
    
                 Scores[N] = Scores[N-1];
    
                Scores[N-1] = temp;
    
                }
    
          }
           
    return temp;  
    
     }     
    
    

     

    this is my output file

    The original list of test scores is:
    Student ID # Test Scores
    2014 73.53
    7602 62.64
    6039 75.00
    9204 93.31
    2054 51.92
    3306 100.00
    8077 84.77
    2308 62.86
    7090 88.58
    1041 45.92
    9118 76.98
    9712 77.21
    2139 78.64
    1465 69.13
    4158 76.32
    6816 82.31
    7175 85.10
    1822 89.00
    8199 74.65
    8520 99.86
    5216 76.27
    2288 95.38
    3235 86.89
    2824 55.82
    7254 92.44
    2661 81.76
    1279 90.98
    the lowest test score was 0126112C and was acheived by student #0031FA80
    the highest test score was 01261186 and was acheived by student #0031FA80
    The average score of the group was 01261613
    01261307

    lost on this part..

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    I don't think you tried to implement what I showed you very hard. You changed a lot of functions to return values but you aren't assigning the values to variables. There are two sentences that account for everything you can do with return values.

    "Store it or ignore it."

    Code:
    int minimal = FindMin(StudID, size);
    // Store it: FindMax is executed and the value returned is stored in minimal. Use minimal.
    
    FindMin(StudID, size);
    // Ignore it: the return value is still returned, but after the sequence point (semicolon) the opportunity to store it is lost forever.
    "Use it or lose it."

    Code:
    cout << FindMin(StudID, size) << endl;
    // Use it: The function is called as part of a larger expression that will use the value returned. 
    
    FindMin(StudID, size);
    // Lose it: the return value is still returned, but after the sequence point it can't be used anymore.
    Also note, choosing to store it means that you don't have to call the function the same way several times, and that difference is rather crucial. Apart from any performance hit, sometimes it isn't even possible to call a function the same way more than once because of side effects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-30-2011, 02:55 PM
  2. Print a 2D Array
    By xxshankar in forum C Programming
    Replies: 3
    Last Post: 03-11-2010, 08:29 PM
  3. first number in array incorrect...
    By sqytoad in forum C Programming
    Replies: 13
    Last Post: 06-09-2008, 12:41 PM
  4. Replies: 2
    Last Post: 04-27-2008, 04:39 AM
  5. How to print an array? Help PLEASE!!!!
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 11-17-2002, 07:23 PM

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