GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-May-2004, 21:55
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

C++ sums and average


Hello everyone,
Need help with modifying a program with following requirements:

1) Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many numbers are in the file.

2) Save the output of the program in a file.

3) Modify the function getNumber so that it reads a number from the input file (opened in the finction main), outputs the number to the output file (opened in the function main), and sends the number read to the function main. Print only 10 numbers per line.

4) Have the program find the sum and average of the numbers.

5) Modify the function printResult so that it outputs the final results to the output file (opened in the function main). Other than outputting the appropriate counts, this new defination of the function printResult should also output the sum and average of the numbers.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>

using namespace std;

const int N = 20;

void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& num);
void classifyNumber(int num, int& zeroCount, int& oddCount, int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);

int main()
{
  int counter;
  int number;
  int zeros;
  int odds;
  int evens;

  initialize(zeros, odds, evens);

  cout<<"Please enter "<<N<<" integers."
      <<endl;
  cout<<"The numbers you entered are--> "<<endl;

  for (counter =1; counter <= N; counter++ )
   {
     getNumber(number);

   cout<<setw(3)<<number;
   classifyNumber(number, zeros, odds, evens);
   }

   cout<<endl;
   printResults(zeros, odds, evens);

   return 0;
}

void initialize(int& zeroCount, int& oddCount, int& evenCount)
  {
   zeroCount = 0;
   oddCount = 0;
   evenCount = 0;
  }

void getNumber(int& num)
  {
   cin>>num;
  }

void classisyNumber(int num, int& zeroCount, int& oddCount, int& evenCount)
  {
switch(num % 2)
    {
    case 0: evenCount++;
    if(num == 0) zeroCount++;
                      break;
    case 1: 
    case -1: oddCount++;
    }
  }

void printResults(int zeroCount, int oddCount, int evenCount)
  {
   cout<<"There are "<<evenCount<<" evens, "<<"which also includes "
       <<"zeroCount<<" zeros"<<endl;
   cout<<"Total number of odds are: "<<oddCount<<endl;
  }
Last edited by dsmith : 14-May-2004 at 07:37.
  #2  
Old 13-May-2004, 22:32
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 168
dabigmooish will become famous soon enough
first try and use [c] and [c/] tags around your code so it's easier to read
second:

You can use fstream.h for file manipulation. Here's a good tutorial that should cover numbers 1, 2, 3, and 5.

sums and averages are basic math. All you have to do is run a while loop that reads until the end of file (EOF) is reached. Add them up as well as keep a count of the number of times the loop is executed. Thats number 4
  #3  
Old 13-May-2004, 23:06
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road
Wink

Don't get it?????


Hey dabigmooish i dont get it when u say use[c] and [c/] tags around my code what exactly do you mean..... even before someone suggested i use crtl F5 to get it color coded and when i tried, it erased everything i had typed...lol...

Well i am sorry for coming across as being very dum.... maybe i am if not i would have got it hun????? So would you please explain to me how to go about...thanx

Quote:
Originally Posted by dabigmooish
first try and use [c] and [c/] tags around your code so it's easier to read
second:

You can use fstream.h for file manipulation. Here's a good http://www.gidforums.com/t-2062.html that should cover numbers 1, 2, 3, and 5.

sums and averages are basic math. All you have to do is run a while loop that reads until the end of file (EOF) is reached. Add them up as well as keep a count of the number of times the loop is executed. Thats number 4
  #4  
Old 13-May-2004, 23:39
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 570
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
The [C] tags make it possible to have hi-lighting and formatting in your code so that it is easy for other forum members to read. You aren't dumb! Almost no one uses the tags on their first post. To use them is very simple. Here's an example of how to use them to make your code easy to read here in the forum.

Before:
[C]
// ...My code goes here...
[/C]

After:
CPP / C++ / C Code:
    // ...My code goes here...
__________________
-Aaron
  #5  
Old 14-May-2004, 00:29
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by homz
Hey dabigmooish i dont get it when u say use[c] and [c/] tags around my code what exactly do you mean.....
Maybe you could read the sticky message at the top of the forum in red that is titled
GIDForums enables New [c] / [c++] bbcode. It's less than half an inch from the "New Thread" button so it's easy to see.

Hey look! I said it nice, didn't I? Well, mostly
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #6  
Old 14-May-2004, 07:04
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
code should be in between [c] and [/ c] tags (without the space between / and the c )
to input and output from and to files, you'll need to use fstream library. #include<fstream> there are a few threads in this forum that cover the basics of doing so. My guess is that you will be dealing with text files in this program. below are the two threads i could find in this forum that would help you. check these out, and try to work on it. and if you need any help, let us know ;-)
https://www.gidforums.com/t-2642.html

https://www.gidforums.com/t-2632.html

btw have you learned arrays yet?
Last edited by JdS : 14-May-2004 at 09:26. Reason: Please enclose internal thread links in [thread]thread_number[/thread]
  #7  
Old 14-May-2004, 09:28
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road
Talking

Thanks Macinated....


No i have not yet gotten into arrays but i kind of have an idea of what arrays are......

btw have you learned arrays yet?[/quote]
  #8  
Old 14-May-2004, 10:08
homz homz is offline
New Member
 
Join Date: May 2004
Location: ohio
Posts: 23
homz is on a distinguished road

Stuck and need help again....


Well as you can see i most definetly need help...got a little but bot sure how to get the input from the infile...giving me error......
CPP / C++ / C Code:
 
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int N = 0;

void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& num);
void classifyNumber(int num, int& zeroCount, int& oddCount, int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);

int main()
{
  ifstream inFile;
  ofstream outFile;
  
  int counter;
  int number;
  int zeros;
  int odds;
  int evens;
  
  inFile.open("c:data.txt");
  outFile.open("c:avg.txt");
  
  inFile>>score;
  outFile<<"Number: "<<score<<endl;

  initialize(zeros, odds, evens);

  cout<<"Please enter "<<N<<" integers."
      <<endl;
  cout<<"The numbers you entered are--> "<<endl;

  for (counter =1; counter <= N; counter++ )
   {
     getNumber(number);

   cout<<setw(3)<<number;
   classifyNumber(number, zeros, odds, evens);
   }

   cout<<endl;
   printResults(zeros, odds, evens);

return 0;
}

well as you can see i have added the c tags this time and hope it works right.....looool...

Quote:
Originally Posted by machinated
code should be in between [c] and [/ c] tags (without the space between / and the c )
to input and output from and to files, you'll need to use fstream library. #include<fstream> there are a few threads in this forum that cover the basics of doing so. My guess is that you will be dealing with text files in this program. below are the two threads i could find in this forum that would help you. check these out, and try to work on it. and if you need any help, let us know ;-)
https://www.gidforums.com/t-2642.html

https://www.gidforums.com/t-2632.html

btw have you learned arrays yet?
  #9  
Old 14-May-2004, 10:36
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
my advice would be to first add the average and sum function and also make the printresults function output average and sum. then after you have got the program working, add the file reading and writing functionality. just post the code when you have it done and then we'll figure out the file part shouldnt take you more than 10 minutes. btw bravo on [c] and [/ c] tags! worked perfectly!
  #10  
Old 14-May-2004, 11:11
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
first thing that we need to take care of when doing file input/output in this program is get rid of the const int N at the top, since we will be reading as many numbers as we can out of a file without knowing how many numbers there are in it. Also we need to change the for loop inside the main to while loop:
while(as long as the file doesn't end)
{
keep getting numbers
keep classifying numbers
}

the rest would remain the same. ok so lets put this in code a little bit:
CPP / C++ / C Code:
while(!infile.eof())  //eof() is a function which returns true when it detects end of file
//so we want to run the while loop till the file ends (while it hasn't ended).

 getNumber(number,infile); //along with the number, we want to pass the file to get number so it can read from it.

   cout<<number<<" ";
   classifyNumber(number, zeros, odds, evens);
}

now way down in your getnumber function,
CPP / C++ / C Code:
void getNumber(int& num, ifstream &inf)
  {
   inf>>num;
  }

so we got the reading from file working, now you have to work on outputting to the file, which is the exact opposite

once again the code to the entire main function:
CPP / C++ / C Code:
int main()
{
  int counter,number,zeros,odds,evens,sum,avg;

  initialize(zeros, odds, evens);

  /*cout<<"Please enter "<<N<<" integers."
      <<endl;
  cout<<"The numbers you entered are--> "<<endl;*/
	
  ifstream infile("data.txt");   //don't have to use infile.open(filename) can do it in declaration itself
 
  //ofstream outfile("avg.txt"); //don't have to use outfile.open(filename)
  
  while(!infile.eof())
   {
     getNumber(number,infile);

   cout<<number<<" ";
   classifyNumber(number, zeros, odds, evens);
   }

   cout<<endl;
   printResults(zeros, odds, evens);

   return 0;
}
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:24.


vBulletin, Copyright © 2000 - 2016, Jelsoft Enterprises Ltd.