GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Oct-2004, 04:13
chriskan76 chriskan76 is offline
New Member
 
Join Date: Oct 2004
Posts: 4
chriskan76 is on a distinguished road

Array.....


Write a program to read in 20 numbers in the range of 0 to 9. Your program should then will compute the average and the mod for all those numbers. After 20 numbers entered, you program will display the average and mod (Most Frequent Number). If the user entered number which is out of range (0 – 9), the program will stop and give the average and mod from the numbers entered previously.

Your output for the program will be as follows

Please Enter Value for Number 1 : 0
Please Enter Value for Number 2 : 2
Please Enter Value for Number 3 : 5
Please Enter Value for Number 4 : 7
Please Enter Value for Number 5 : 6
Please Enter Value for Number 6 : 2
Please Enter Value for Number 7 : 9
Please Enter Value for Number 8 : 2
Please Enter Value for Number 9 : 4
Please Enter Value for Number 10 : 2
Please Enter Value for Number 11 : 9
Please Enter Value for Number 12 : 0
Please Enter Value for Number 13 : 2
Please Enter Value for Number 14 : 5
Please Enter Value for Number 15 : 3
Please Enter Value for Number 16 : 8
Please Enter Value for Number 17 : 6
Please Enter Value for Number 18 : 2
Please Enter Value for Number 19 : 1
Please Enter Value for Number 20 : 9

The Average is 4.2
The Mod is 2

If the user entered number which is out of range (0 – 9), the program will stop and give the average and mod from the numbers entered previously.


i am newbie to c...can anyone help me to solve this problem....?????
  #2  
Old 18-Oct-2004, 04:41
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
This looks like a programming assignment. I would suggest you to post some code with specific problem that you are facing. I hope you are aware of arrays and loops. I can give you some logic to work on but you have to write code by yourself.

Here we go..

define an interger array of size 20 to hold the 20 numbers entered by user.

define a second integer array of size 10 to hold the frequency of each number between 0 to 9 for e.g a[0] will hold the freqency of 0, a[1] will hold frequency of 1 and so on till a[9]..

start a counter variable to 0

read in 20 numbers in using while loop. Check for counter is less than equal to 20 and entered value is in the range from 0 to 9. Update the frequecy array by incrementing corresponding value by 1. For eg if user enters 2 then increment the value stored in a[2] of freqeuncy array by 1.

Initialize sum to 0.

initialize a second counter to 0

Loop through integer array and calculate sum of all 20 elements in array and then calculate average. Store the average in seperate variable and print it.

find out the maximum found number from frequency array and print out the corresponding array index value which will the the "Most frequent number".
  #3  
Old 18-Oct-2004, 04:54
chriskan76 chriskan76 is offline
New Member
 
Join Date: Oct 2004
Posts: 4
chriskan76 is on a distinguished road

Open file...


thanks for the information....i have another program which i cant compute the average salary(sum of the totalpay/5??) for the 5 input data, is there something wrong with my loop??can u check my program code...???

CPP / C++ / C Code:
#include <stdio.h>

void main (void)
{
	int id, did, hours;
	float rate, totalpay, overtime;
	char status;
	int i;

	FILE *fp;
	i = 1;
	fp = fopen("c:/data.txt", "w");

	while (i++ <= 5)
	{
		printf("Please Key In Employer ID :");
		scanf("%d", &id);
		printf("Please Key In Department No :");
		scanf("%d", &did);
		printf("Please Key In Employer Pay Rate :");
		scanf("%f", &rate);
		printf("Please Key In Employer Status(Y/N) :");
		scanf(" %c", &status);
		printf("Please Key In No Hours :");
		scanf("%d", &hours);

		fprintf(fp, "%d %d %4.2f %c %d\n", id, did, rate, status, hours);
	}

	fclose(fp);
	fp = fopen("c:\data.txt", "r");

	i = 1;
	printf("\n\nReading from file\n");
	while (i++ <= 5)
	{
		fscanf(fp, "%d %d %f %c %d", &id, &did, &rate, &status, &hours);

		if (status == 'Y')
		{
			if(hours>=40)
				{
					totalpay = hours * rate;
					overtime=0.0;
				}
			else
				{
				  totalpay = hours * rate;
				}
		}
		else if (status == 'N')
		{
			if (hours >= 40)
			{
				overtime= (hours - 40) * 1.5 * rate;
				totalpay = overtime+ (40 * rate);
			}
			else
			{
				totalpay = hours * rate;
			}

		}




		printf("%4d %d %4.2f %c %d %3.2f %4.2f\n", id, did, rate, status, hours, overtime, totalpay);

	 }
	fclose(fp);
Last edited by LuciWiz : 18-Oct-2004 at 05:08. Reason: Please insert your C code between [c] [/c] tags
  #4  
Old 18-Oct-2004, 05:08
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi,

Pls keep one problem in one post. Start another post for new problem. Also use code tag to maintain the code indentation. Its difficult to read the code without code tags. Read stickys for new users on how to use code tags.

As for the problem you mentioned, you are not showing where and how you care calculating average salary.

Also, one imp thing about your program is that you are not checking the return value of fopen(). Always check for NULL for file open operation as it may crash your program.

Always check fopen as

CPP / C++ / C Code:

if ((fp = fopen("c:/data.txt", "w"))==NULL)
{
printf("data.txt open error\n");
exit(1);
}



Thanks,
  #5  
Old 18-Oct-2004, 15:19
mikhail mikhail is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: Dublin, Ireland
Posts: 73
mikhail is on a distinguished road
In the line:
CPP / C++ / C Code:
    fscanf(fp, "%d %d %f %c %d", &id, &did, &rate, &status, &hours);
, which occurs near the start of the loop to read the data back from the file, you should be reading back the \n at the end of the line.

Strange really, that's the sort of problem I would have had when coding in my first or second year in college - are there no TAs about to look at your code?
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
more array help (simulation project..) dilmv C++ Forum 6 17-Oct-2004 08:51
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 22:52
c: array comparison jack C Programming Language 7 26-Jan-2004 12:21
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 12:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:15.


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