College Lesson Trouble With Count/Sentinel Control Loops - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27

Thread: College Lesson Trouble With Count/Sentinel Control Loops

  1. #16
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by GCDEF View Post
    I hadn't read the original assignment about performing the tasks separately, but he shows you what you need to do. You will need multiple loops closing and reopening the file between them.

    I've answered your question about repeating the last number several times now. You were on the right track earlier when you read the file before your loop, but you need to put the output before the input in the loop.
    Ok I got it =P

    Code:
    total=0;
    	counter=0;
    	filein>>num;
    	while(filein)
    	{	
    		
    		fileout<<num<<" "; 
    		filein>>num;
    		total=total+num;
    		counter++;
    	}
    
    	average = total/counter;
    	fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
    Chapter 8 assignment is where we do multiple loops using void functions... So I guess I need to do a separate print out for each one.

  2. #17
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,093

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Looks like all the steps, a, b, c, d... he asks you to "reread" the file, which would require separate loops and closing and opening the file between each loop.

  3. #18
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,565

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    concerning reading last value twice ... you have

    Code:
    while (filein)
    {	
       filein>>num;
       fileout<<num<<" "; 
       total=total+num;
       counter++;
    }
    You are reading the last value, and adding to total (so far so good).

    Then it goes back to the start of the loop : while (filein)
    This still will result in "true", so you enter the body of the loop again.

    generally, you should loop as follows:

    Code:
    while (filein >> num)
    {
       total += num;
       ++counter;
    }
    Last edited by Philip Nicoletti; October 31st, 2012 at 10:02 AM.

  4. #19
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Whoa... look at me go lol Thanks!

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    void main()
    {
    	int num, fnum, lnum, sum;
    	float average, total, counter;
    	char inchar;
    	ofstream fileout;
    	ifstream filein;
    
    	fileout.open("E:\\Resource Files\\answers.txt");
    	filein.open("E:\\Resource Files\\datafile2.txt");
    	
    
    
    	total=0;
    	counter=0;
    	filein>>num;
    	while(filein)
    	{	
    		
    		fileout<<num<<" "; 
    		filein>>num;
    		total=total+num;
    		counter++;
    	}
    
    	average = total/counter;
    	fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
    
    	filein.close();
    	filein.clear();
    	
    	filein.open("E:\\Resource Files\\datafile2.txt");
    
    	total=0;
    	counter=1;
    	while(counter <=12)
    	{
    			filein>>num;
    			total=total+num;
    			counter++;
    	}
    
    	average=total/12;
    	fileout<<"The average of the first 12 integers of the input file is: "<<average<<endl;
    
    
    		
    }
    Code:
    55 67 458 23 81 33 782 375 528 405 324 950 46 14 864 551 38 167 518 630 
    The Average from the input file is: 374.200
    The average of the first 12 integers of the input file is: 340.083

  5. #20
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Ok, Now I had friend give me a hint on this next piece for reading every 3rd integer in the input file, but I don't know how to decipher it.

    if(num%3==0)

    I get that I will need an "if" statement, but what does "num%3==0" mean?!

  6. #21
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,093

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Remember elementary school division? 5 divided by 3 = 1 remainder 2, for example. The modulo operator gives you the remainder, so 5 % 3 would return 2. If it equals zero, the numerator is evenly divisible by the denominator.

  7. #22
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by GCDEF View Post
    Remember elementary school division? 5 divided by 3 = 1 remainder 2, for example. The modulo operator gives you the remainder, so 5 % 3 would return 2. If it equals zero, the numerator is evenly divisible by the denominator.
    Ok, so the if statements expression is only true if the remainder of the num%3 equals 0. So if there is no remainder than it is a true statement.

  8. #23
    Join Date
    Oct 2012
    Posts
    14

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Pretty sure I got the code right, but I am coming up with a different answer than what I was expecting

    Code:
    55 67 458 23 81 33 782 375 528 405 324 950 46 14 864 551 38 167 518 630 
    The Average from the input file is: 374.200
    The average of the first 12 integers of the input file is: 340.083
    The sum of every third number is: 3870.000
    Code:
    	filein.close();
    	filein.clear();
    	
    	filein.open("E:\\Resource Files\\datafile2.txt");
    
    	total=0;
    	while(filein)
    	{
    		filein>>num;
    		if(num%3==0)
    		total=total+num;
    	}
    
    	fileout<<"The sum of every third number is: "<<total<<endl;
    		
    }
    From adding every 3rd number I am getting 3000 as my answer.

  9. #24
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,093

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by commiedic View Post
    Ok, so the if statements expression is only true if the remainder of the num%3 equals 0. So if there is no remainder than it is a true statement.
    Yes. Typically C and C++ programmers start counters at 0, so if your loop included if(counter % 3) == 0) then every third time through the loop, beginning with the first time, would be true.

  10. #25
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,093

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by commiedic View Post
    Pretty sure I got the code right, but I am coming up with a different answer than what I was expecting

    Code:
    55 67 458 23 81 33 782 375 528 405 324 950 46 14 864 551 38 167 518 630 
    The Average from the input file is: 374.200
    The average of the first 12 integers of the input file is: 340.083
    The sum of every third number is: 3870.000
    Code:
    	filein.close();
    	filein.clear();
    	
    	filein.open("E:\\Resource Files\\datafile2.txt");
    
    	total=0;
    	while(filein)
    	{
    		filein>>num;
    		if(num%3==0)
    		total=total+num;
    	}
    
    	fileout<<"The sum of every third number is: "<<total<<endl;
    		
    }
    From adding every 3rd number I am getting 3000 as my answer.
    You really need to think about what you're doing. num could be anything. num isn't the value you want to be testing here.

  11. #26
    Join Date
    Oct 2012
    Posts
    17

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Quote Originally Posted by commiedic View Post
    Ok I got it =P

    Code:
    total=0;
    	counter=0;
    	filein>>num;
    	while(filein)
    	{	
    		
    		fileout<<num<<" "; 
    		filein>>num;
    		total=total+num;
    		counter++;
    	}
    
    	average = total/counter;
    	fileout<<fixed<<setprecision(3)<<endl<<"The Average from the input file is: "<<average<<endl;
    Chapter 8 assignment is where we do multiple loops using void functions... So I guess I need to do a separate print out for each one.
    Would an eof() function have worked here as well?

  12. #27
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,565

    Re: College Lesson Trouble With Count/Sentinel Control Loops

    Before worrying about every third number, you should get
    the very first part correct: getting the average of all the numbers
    in the file.

    You have 2 ways of doing that:

    1) Take away the read outside of the while loop, and use the while
    loop that I posted. (best method)

    2) keep you loop as is ... but inside the loop check the state
    of the stream. If the stream's state is false, do not add num to total.

    l

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center