Thread: i have a problem on my program, please help!

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    20

    i have a problem on my program, please help!

    my program suppose input:
    23 5 87 -3 29 87 0 104 300 100 55 -34 -3 101 33 23

    suppose output the bad scores:
    -3 104 300 -34 -3 101

    suppose output the good scores:
    23 5 87 29 87 0 100 55 33 23

    when i calculate the sum of good scores, and finding the average of the good scores, it cannot output the correct scores.

    here is the code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    #define inFile "c:\\input.txt"
    #define outFile "c:\\output.txt"
    
    const int arraySize = 16;
    
    void getInput(ifstream& indata, int list[], int listSize);
    void badSocres(ofstream& outdata, int listOne[], int listTwo[], int listSize);
    void goodScores(ofstream& outdata, int listOne[], int listThree[], int listSize);
    void calScores(ofstream& outdata, int listThree[]);
    
    int main ()
    {
    	ifstream indata;
    	ofstream outdata;
    	
    
    	int listA[arraySize];
    	int listB[arraySize];
    	int listC[arraySize];
    
    
    
    	indata.open(inFile);
    	if (indata.fail())
    	{
    		cout << "***ERROR:Cannot open" << inFile
    			 << " for input." << endl;
    		return EXIT_FAILURE;
    	}
    
    	outdata.open(outFile);
    	if (outdata.fail())
    	{
    		cout << "***ERROR:Cannot open" << outFile
    			 << " for output." << endl;
    		indata.close();
    		return EXIT_FAILURE;
    	}
    
    	if (indata.eof())
    	{
    		cout << "Sorry there are no integers" << endl;
    		indata.close();
    		outdata.close();
    		return EXIT_FAILURE;
    	}
    
    	getInput(indata, listA, arraySize);
    
    	
    	badSocres(outdata, listA, listB, arraySize);
    
    	goodScores(outdata, listA, listC, arraySize);
    
    	calScores(outdata, listC);
    
    
    
    	indata.close();
    	outdata.close();
    
    	return 0;
    
    }
    
    void getInput(ifstream& indata, int list[], int listSize)
    {
    	int index;
    
    	for (index = 0; index < listSize; index++)
    		indata >> list[index];
    }
    
    void badSocres(ofstream& outdata, int listOne[], int listTwo[], int listSize)
    {
    	int index;
        
    	outdata << "6 bad scores:" << endl;
    	
    
    	for (index = 0; index < listSize; index++)
    	{
            if(listOne[index] < 0 || listOne[index] > 100) 
    		{
              listTwo[index] = listOne[index];
              outdata << setw(4) << listTwo [index];
    		}
    	}
    	    
    }
    
    void goodScores(ofstream& outdata, int listOne[], int listThree[], int listSize)
    {
    	int index;
    
    	outdata << endl;
    	outdata << "10 good scores:" << endl;
    
    	for (index = 0; index < listSize; index++)
    	{
    		if(listOne[index] >= 0 && listOne[index] <= 100)
    		{
    			listThree[index] = listOne[index];
    			outdata << setw(4) << listThree[index];
    			
    		}
    		
    	}
    }
    
    void calScores(ofstream& outdata, int listThree[])
    {
    	int index, average;
        int sum = 0;
    
    	outdata << endl;
    
    	for (index = 0; index < 10; index++)
    	{
    		sum = sum + listThree[index];
    		
    		if(listThree[index] == 100)
    		{
    			
    			outdata << "The hightest good score is:" << listThree[index];
    		    outdata << endl;
    		}
    
    		
    
    		if(listThree[index] == 0)
    		{
    			
    			outdata << "The lowest good socre is:" << listThree[index];
    		    outdata << endl;
    		}
    	
    		
    	}
    
    	    average = sum / index;
    		outdata << "The average of good scores is:" << average;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    average = sum / index;
    This is integer math.

    What is the output? The real output, not the intended output.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Change
    Code:
    average = sum / index;
    		outdata << "The average of good scores is:" << average;
    ->
    Code:
    outdata << "The average of good scores is: " << (double)sum / index;
    and see what happens. (My version uses floating-point math instead of integer.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One other note: there's a better way to calculate the highest number in an array:
    Code:
    int x, high = 0;
    
    for(x = 0; x < max; x ++) {
        if(array[x] > array[high]) high = x;
    }
    
    // now high is the highest element
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Something else you might want to watch. This code
    Code:
    		cout << "***ERROR:Cannot open" << outFile
    			 << " for output." << endl;
    will print
    Code:
    ***ERROR:Cannot openc:\output.txt for output
    which is probably not what you want. You need to print a space before the file.

    This happens several other times in that program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One more thing (honest): the code
    Code:
    average = sum / index;
    should probably use 10, or something, instead of index. index will be 11, which is probably not what you want.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    the average output suppose is 44.2

    but i was change the Code:
    outdata << "The average of good scores is: " << (double)sum / index;

    it also cannot output the correct answer.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I wanted to know what the average output is, not what it is supposed to be.

    ... i was change the Code:
    outdata << "The average of good scores is: " << (double)sum / index;

    it also cannot output the correct answer.
    See my other post:
    Quote Originally Posted by dwks
    One more thing (honest): the code

    Code:
    average = sum / index;
    should probably use 10, or something, instead of index. index will be 11, which is probably not what you want.
    Therefore, the code should be (assuming there's always 10 numbers)
    Code:
    outdata << "The average of good scores is: " << (double)sum / 10;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    the average output is 8.58994e+007 when i change the code (double)sum / 10.
    Thanks!

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is that what you want? If so, you're welcome. You should also read my other posts, especially the one about finding the maximum number in an array.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    i think no, it should be output 44.2.
    so i don't know how to do that.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hmm, your value looks wacky.

    How about you use this
    Code:
    outdata << sum << endl;
    to see what sum is. If you want the average to be 44.2, sum should be 442 (44.2 * 10).

    Post your whole code again so I can see the changes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    Quote Originally Posted by dwks
    Hmm, your value looks wacky.

    How about you use this
    Code:
    outdata << sum << endl;
    to see what sum is. If you want the average to be 44.2, sum should be 442 (44.2 * 10).

    Post your whole code again so I can see the changes.
    i think so!!!

    here is the code that i was changed:
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    #define inFile "c:\\input.txt"
    #define outFile "c:\\output.txt"
    
    const int arraySize = 16;
    
    void getInput(ifstream& indata, int list[], int listSize);
    void badSocres(ofstream& outdata, int listOne[], int listTwo[], int listSize);
    void goodScores(ofstream& outdata, int listOne[], int listThree[], int listSize);
    void calScores(ofstream& outdata, int listThree[]);
    
    int main ()
    {
    	ifstream indata;
    	ofstream outdata;
    
    	
    
    	int listA[arraySize];
    	int listB[arraySize];
    	int listC[arraySize];
    
    
    
    	indata.open(inFile);
    	if (indata.fail())
    	{
    		cout << "***ERROR:Cannot open " << inFile
    			 << " for input." << endl;
    		return EXIT_FAILURE;
    	}
    
    	outdata.open(outFile);
    	if (outdata.fail())
    	{
    		cout << "***ERROR:Cannot open " << outFile
    			 << " for output." << endl;
    		indata.close();
    		return EXIT_FAILURE;
    	}
    
    	if (indata.eof())
    	{
    		cout << "Sorry there are no integers" << endl;
    		indata.close();
    		outdata.close();
    		return EXIT_FAILURE;
    	}
    
    	getInput(indata, listA, arraySize);
    
    	
    	badSocres(outdata, listA, listB, arraySize);
    
    	goodScores(outdata, listA, listC, arraySize);
    
    	calScores(outdata, listC);
    
    
    
    	indata.close();
    	outdata.close();
    
    	return 0;
    
    }
    
    void getInput(ifstream& indata, int list[], int listSize)
    {
    	int index;
    
    	for (index = 0; index < listSize; index++)
    		indata >> list[index];
    }
    
    void badSocres(ofstream& outdata, int listOne[], int listTwo[], int listSize)
    {
    	int index;
        
    	outdata << "6 bad scores:" << endl;
    	
    
    	for (index = 0; index < listSize; index++)
    	{
            if(listOne[index] < 0 || listOne[index] > 100) 
    		{
              listTwo[index] = listOne[index];
              outdata << setw(4) << listTwo [index];
    		}
    	}
    	    
    }
    
    void goodScores(ofstream& outdata, int listOne[], int listThree[], int listSize)
    {
    	int index;
    
    	outdata << endl;
    	outdata << "10 good scores:" << endl;
    
    	for (index = 0; index < listSize; index++)
    	{
    		if(listOne[index] >= 0 && listOne[index] <= 100)
    		{
    			listThree[index] = listOne[index];
    			outdata << setw(4) << listThree[index];
    			
    		}
    		
    	}
    }
    
    void calScores(ofstream& outdata, int listThree[])
    {
    	int index;
    	
        int sum;
    
    	outdata << endl;
    
    	for (index = 0; index < 10; index++)
    	{
    		sum = sum + listThree[index];
    		
    	    if(listThree[index] == 100)
    		{
    			
    			outdata << "The hightest good score is:" << listThree[index];
    		    outdata << endl;
    		}
    
    
    		if(listThree[index] == 0)
    		{
    			
    			outdata << "The lowest good socre is:" << listThree[index];
    		    outdata << endl;
    		}
    	
    		
    	}
    
    	    outdata << "The average of good scores is:" << double(sum/10);
    }

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This code
    Code:
    double(sum/10)
    is wrong. I said to use
    Code:
    (double)sum/10
    (or here's an equivalent
    Code:
    sum/10.0
    ).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM