C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 25th, 2009, 10:46 PM
2006C++ 2006C++ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 1 2006C++ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 11 sec
Reputation Power: 0
Memory & arrays - C++ help writing program that can output mode using arrays?

I am having trouble getting the program to ouput the number of scores for example score 78 count 2; which means there was two 78 inputed by the user. I am also having trouble getting the mode to work. I had it almost correct at one put but then I fiddled with it and forgot what I had. Also, I need it to only list the 78 once with a 2 next to it. And the mode must list all if their is more then one, I do not need to consider senerios where there is no mode.


Code:
#include <iostream>
#include <iomanip>
using namespace std;

int main () {

int input[50];
int count[50];
int i, j, n, tmp, mode, highest, lowest, sum, average;


n=0;


cout << "Input grades in order to find mode. " << endl << "(-99 to exit)" << endl;
cin >> input[n];

while (input[n] != -99) {
n++;
cout << "Input grades in order to find mode. " << endl << "(-99 to exit)" << endl;
cin >> input[n];
cout << endl;
}

cout << endl;

for (j=0;j<n-1;j++){
for (i=0;i<n-1;i++)
if (input[i+1]<input[i]) {
tmp=input[i];
input[i]=input[i+1];
input[i+1]=tmp;
}}

for (i=0;i<n;i++) {
count[i]=1;
if (input[i]==input[i+1])
count[i]++;
}

cout << endl;

sum= 0;
highest= 0;
lowest= 110;

for (i=0; i < n; i++) {
sum = sum + input[i];
if (input[i] > highest)
highest = input[i];
if (input[i] < lowest)
lowest = input[i];
}


average = sum/n;

mode=0;
for (i=0; i<n; i++) {
if (count[i] > mode)
mode = input[i];
}


cout << "The " << n << " temperatures are: " << endl;
cout << endl;


cout << "Test Scores Count" << endl;
cout << "----------- -----" << endl;

for (i=0; i < n; i++) {
cout << setw(6) << input[i] << setw(15) << count[i] << endl;
}

cout << endl;
cout << "Lowest temperature: " << lowest << endl;
cout << "Highest temperature: " << highest << endl;
cout << "Average temperature: " << average << endl;
cout << "Mode: " << mode << endl;
cout << endl;

return 0;
}

Reply With Quote
  #2  
Old March 26th, 2009, 02:04 AM
monster monster is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 127 monster User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 20 m 59 sec
Reputation Power: 9
Quote:
Originally Posted by 2006C++
I am having trouble getting the program to ouput the number of scores for example score 78 count 2; which means there was two 78 inputed by the user. I am also having trouble getting the mode to work. I had it almost correct at one put but then I fiddled with it and forgot what I had. Also, I need it to only list the 78 once with a 2 next to it. And the mode must list all if their is more then one, I do not need to consider senerios where there is no mode.


Code:
#include <iostream>
#include <iomanip>
using namespace std;

int main () {

int input[50];
int count[50];
int i, j, n, tmp, mode, highest, lowest, sum, average;


n=0;


cout << "Input grades in order to find mode. " << endl << "(-99 to exit)" << endl;
cin >> input[n];

while (input[n] != -99) {
n++;
cout << "Input grades in order to find mode. " << endl << "(-99 to exit)" << endl;
cin >> input[n];
cout << endl;
}

cout << endl;

for (j=0;j<n-1;j++){
for (i=0;i<n-1;i++)
if (input[i+1]<input[i]) {
tmp=input[i];
input[i]=input[i+1];
input[i+1]=tmp;
}}

for (i=0;i<n;i++) {
count[i]=1;
if (input[i]==input[i+1])
count[i]++;
}

cout << endl;

sum= 0;
highest= 0;
lowest= 110;

for (i=0; i < n; i++) {
sum = sum + input[i];
if (input[i] > highest)
highest = input[i];
if (input[i] < lowest)
lowest = input[i];
}


average = sum/n;

mode=0;
for (i=0; i<n; i++) {
if (count[i] > mode)
mode = input[i];
}


cout << "The " << n << " temperatures are: " << endl;
cout << endl;


cout << "Test Scores Count" << endl;
cout << "----------- -----" << endl;

for (i=0; i < n; i++) {
cout << setw(6) << input[i] << setw(15) << count[i] << endl;
}

cout << endl;
cout << "Lowest temperature: " << lowest << endl;
cout << "Highest temperature: " << highest << endl;
cout << "Average temperature: " << average << endl;
cout << "Mode: " << mode << endl;
cout << endl;

return 0;
}


I just thought of this, but maybe you could have an array of 101 or something indexed by the score.

Then default the array to 0, and if you the user inputs a number, just increment the element of the array.

eg.

int score[101];

//user inputs 49

score[49] += 1;

this way you can see how many times each number is inputed, and will be much easier to figure out the mode.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - C++ help writing program that can output mode using arrays?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap