473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

rewrite this program (sum,average) using functions, possibly arrays?

rewrite this program (sum,average) using functions, possibly arrays?


#include<stdlib.h>
#include<stdio.h>

main()
{
FILE *finpt,
*foutpt;

char ch;
int i,
scr1,scr2,scr3, testSum1=0,testSum2=0,testSum3=0;
float studcount=0.0;

finpt=fopen("tests.txt","r"); foutpt=fopen("copy.txt","w");

fprintf(foutpt,"\nSTUDENT\t\t\tTEST 1\tTEST 2\tTEST 3\t STUDENT AVERAGE\n\n");


while ( fscanf ( finpt," %c",&ch) != EOF) {
fprintf(foutpt,"%c",ch);

for ( i=2; i<=15; i++) {
fscanf(finpt,"%c",&ch);
fprintf(foutpt,"%c",ch);
}

fprintf(foutpt," ");

float avgRow=0;

fscanf(finpt,"%d%d%d",&scr1,&scr2,&scr3);
fprintf(foutpt,"%8d%8d%8d",scr1,scr2,scr3);
fprintf(foutpt,"%16.1f",avgRow=(scr1+scr2+scr3)/3.0);
testSum1+=scr1; testSum2+=scr2; testSum3+=scr3;
studcount++;

fprintf(foutpt,"\n");
}

float avgTest1=testSum1/studcount;
float avgTest2=testSum2/studcount;
float avgTest3=testSum3/studcount;
fprintf(foutpt,"\nTEST AVERAGE%18.1f%8.1f%8.1f",avgTest1,avgTest2,avgTest 3);

fclose(finpt); fclose(foutpt);
return EXIT_SUCCESS;
}
Nov 27 '06 #1
3 2867
sicarie
4,677 Expert Mod 4TB
What's your question? Are you wondering where to start, or do you have a question about passing variables (such as an array) to a function?
Nov 27 '06 #2
basically....

how would i go about using functions for summing up the test columns, instead of reading each column one by one and summing up individually

ie: i want to use the following while statement:

while( student(&testSum1,&testSum2,&testSum3,&studCount) !=EOF) ;

..after the last student record is read and processed, i want to use another function for the average,

testAvgs(testSum1,testSum2,testSum3,studCount)
Nov 27 '06 #3
sicarie
4,677 Expert Mod 4TB
basically....

how would i go about using functions for summing up the test columns, instead of reading each column one by one and summing up individually

ie: i want to use the following while statement:

while( student(&testSum1,&testSum2,&testSum3,&studCount) !=EOF) ;

..after the last student record is read and processed, i want to use another function for the average,

testAvgs(testSum1,testSum2,testSum3,studCount)

Well, you're looking for two functions, right, sum and average? So it would seem that you would just want to read all the values in (an array would be ideal there), and then pass that to a function, instead of each individual value. That would return an int and take an int[] as a parameter, and internally add all of them into a tempvar. You're looking like you can declare functions above, so I'm going to leave the actual declaration to you, and then average can just take the sum and the size and return the average.

That while statement is a little odd. I take it from the EOF that you're reading them in from a file, but I'd remove the function call from there - it doesn't seem like that will ever be EOF (though it might be null).

You can use getline or a file input stream, depending on how you call it (something like cin >> var, only not using cin). Then declare a counter variable and increment it each time the while loop is executed, use that for the index of your array - scores[i] = varFromFile; i++;

Does that make sense?
Nov 27 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
1
by: madstyle51 | last post by:
#include <stdio.h> #include <math.h> /* Define the variables. */ int main () { int N; float Q; float max;
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
1
by: sparkid | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
4
by: sparkid | last post by:
I do not figure out how to print the letter grades in functions. The program below compute the average of 6 grades or GPA and its standard deviation. The thing i cant figure out how to print the...
6
by: phkram | last post by:
Hello everybody... i find this place very helpful. anyone who can help me solve the following problems? Thank you very much for helping me.. 1. Build a header file that consists of a class...
6
by: compsci | last post by:
I'm trying to write a program that does the following: 1. Repeatedly read a student score (for an exam) and add it to a running sum. Count the number of scores entered. (We’ll assume that the...
2
by: Nusc | last post by:
The following program is supposed to prompt the user for a student ID. Open a text file. Calculate the student's average GPA and print his transcript. #include <fstream> #include <vector>...
3
by: powerej | last post by:
writing a program that asks how many numbers the users want to enter, then input an integer for the amount of numbers said, output: number of integers entered, sum of them, average of them, maximum...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.