Reply
Old 03-11-2013, 03:04 AM   #1 (permalink)
ace
 
Status: Member
Posts: 83



Default c programming hmwk help

First create a text file that has between 8 and 12 integers typed in each line of the file. Save this file to a location of your own choice and you may use a name of your choice.
Then write program to open up the file, read the file until end-of-file, then report the following (to the screen):
a) Count of all numbers entered. (in a function)
b) Sum of all numbers entered. (in a function)



I know how to create the text file and read and write it. I am confused on how to find the count of all the numbers and sum of all numbers...
My numbers in the text file are:
1
2
3
4
5
0
0
0
-2
-2

Here's what I have so far:
#include <stdio.h>
#include <stdlib.h>

void getCount(); //function
void calcSum(); //function

int main (void)
{
//Local Declarations
FILE* spIn;
FILE* spOut; //for output file
int numIn;
int countNum, sum;

spIn = fopen("c:\\aa\\question1.txt", "r");
if (!spIn)
{
printf("Could not open file\a\a\a\n");
exit (101);
} // if open fail

spOut = fopen("c:\\aa\\outData.txt", "w");
if (!spOut)
{
printf("Could not open file\a\a\a\n");
exit (101);
} // if open fail

while ((fscanf(spIn, "%d", &numIn)) == 1) {
fprintf(stdout,"\n%d ", numIn);
fprintf(spOut,"\n%d ", numIn); //write to output file
}
return 0;
}

void getCount() //My attempt to get the count of numbers...
{
int i;
int countNum;

for(i=0;i<numIn;i++) {
if(numIn) {
countNum++;
}
}
return;
}
ace is offline   Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java hmwk help!? yishunshin Programming Languages 0 03-07-2014 02:05 AM
Java programming hmwk help. Done with overall, just need help on last part.? ace Billing Coding 0 09-16-2013 02:05 AM
c programming hmwk (need help)? ace Coding 1 03-06-2013 09:04 PM
c programming hmwk (need help)? ace Coding 1 03-04-2013 06:06 AM
Beginner Java hmwk help!? Unknown_User Coding 3 01-30-2012 04:36 AM