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 19-Oct-2004, 20:13
chriskan76 chriskan76 is offline
New Member
 
Join Date: Oct 2004
Posts: 4
chriskan76 is on a distinguished road

Anyone can write a program code for this???


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.

Please Enter Value for Number 1 : 5
Please Enter Value for Number 2 : 5
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 : 5
Please Enter Value for Number 11 : 11

The Average is 5.0
The Mod is 5



I am stuck with the mod(most frequent number). Any idea??
  #2  
Old 19-Oct-2004, 20:25
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi chriskan76.

To get the mod just use an array of 10 elements that is cleared to 0. Index through each number in your list and use the array to store how many times you encounter each number. The arrray element with the highest number wins!

Here's some snippets that may (or may not ) help.
CPP / C++ / C Code:
  int count[10];
  int loop;
  int mode;
  int mode_counter;

  //Clear the count array.
  for(loop = 0; loop<10; loop++)
    count[loop] = 0;

  //Count the numbers
  for(loop = 0; loop<20, numbers[loop]<10; loop++)
    count[numbers[loop]]++;

  mode_counter = 0;  //This will keep track of the count for the highest encountered number
  for(loop = 0; loop<10; loop++)
    if(count[loop] > mode_counter){
      mode = loop;
      mode_counter = count[loop];
    }

Unfortunately, you probably should think about multi-modal data sets as well. The above code will only give the first encountered mode.

Good luck.
 


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
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
very difficult code - program gaurav_sting C++ Forum 1 16-Jun-2004 00:59
Plz help me to write this program.. X-Man C Programming Language 9 10-May-2004 09:07
How to write a code for froggyma C++ Forum 1 29-Mar-2004 00:54
Will pay money for someone to write a semi complex program bstan Computer Programming Advertisements & Offers 0 23-Feb-2004 04:17

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

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


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