473,804 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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,test Sum2=0,testSum3 =0;
float studcount=0.0;

finpt=fopen("te sts.txt","r"); foutpt=fopen("c opy.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,&s cr2,&scr3);
fprintf(foutpt, "%8d%8d%8d",scr 1,scr2,scr3);
fprintf(foutpt, "%16.1f",avgRow =(scr1+scr2+scr 3)/3.0);
testSum1+=scr1; testSum2+=scr2; testSum3+=scr3;
studcount++;

fprintf(foutpt, "\n");
}

float avgTest1=testSu m1/studcount;
float avgTest2=testSu m2/studcount;
float avgTest3=testSu m3/studcount;
fprintf(foutpt, "\nTEST AVERAGE%18.1f%8 .1f%8.1f",avgTe st1,avgTest2,av gTest3);

fclose(finpt); fclose(foutpt);
return EXIT_SUCCESS;
}
Nov 27 '06 #1
3 2889
sicarie
4,677 Recognized Expert Moderator Specialist
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
bxstylez
2 New Member
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(&testSu m1,&testSum2,&t estSum3,&studCo unt) !=EOF) ;

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

testAvgs(testSu m1,testSum2,tes tSum3,studCount )
Nov 27 '06 #3
sicarie
4,677 Recognized Expert Moderator Specialist
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(&testSu m1,&testSum2,&t estSum3,&studCo unt) !=EOF) ;

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

testAvgs(testSu m1,testSum2,tes tSum3,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
6908
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 Enter a number : 30 More numbers (y/n)? n
1
1987
by: madstyle51 | last post by:
#include <stdio.h> #include <math.h> /* Define the variables. */ int main () { int N; float Q; float max;
21
2559
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 run, However, i simply created this program in arrays and it runs good except i cant figure out how to compute the standard deviation. The coding is below. Any help will be appreciated. 1) The Program will prompt the user for six grades to be...
1
3566
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 run, However, i simply created this program in arrays and it runs good except i cant figure out how to compute the standard deviation. The coding is below. Any help will be appreciated. 1) The Program will prompt the user for six grades to be...
4
2125
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 Letter Grades from 'A' to 'F' to the following equivalences: 3.2 < grade <=4.0 : A 2.4 < grade <=3.2 : B 1.6 < grade <=2.4 : C 0.8 < grade <=3.2 : D 0 <= grade <= 0.8 : F I know it can be done by if statments but cant figure out how to...
6
4343
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 Integer. The class should be capable of computing the sum, difference, product, quotient, and factorials of two numbers. Build an implementation file that uses the newly-built ADT(Abstract Data Type) Integer 2. Write a program that will determine the...
6
1762
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 scores are between 0 and 100.) Stop when the user enters a negative number. Output the sum and count of the scores. 2. Define a function average that, passed the sum of the scores and the count of the scores, computes the average score, which it...
2
1757
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> #include <iostream> #include <cmath> #include <cstdlib>
3
1862
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 value entered and min value entered. the min is where i am having a problem i can get everything else to print out but i am doing something wrong in my while loop can someone please help me. import javax.swing.*; // gets option pane ...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.