InfuzionGaming.com
May 19, 2012, 11:01:03 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

Shout Box

[History] [Commands]

[April 08, 2012, 11:40:11 AM] Unpayed_overtime: *Sneezes*

[April 12, 2012, 04:04:25 PM] Lockem Shoto: Holy crap - the most retarded sh it happened

[April 12, 2012, 04:04:34 PM] Lockem Shoto: I was on a CSS server and apparently they LOVE communism

[April 12, 2012, 04:04:37 PM] Lockem Shoto: They gave admin to everybody

[April 12, 2012, 04:04:41 PM] Lockem Shoto: Nobody abuses for some reason

[April 12, 2012, 04:04:44 PM] Lockem Shoto: I"m surprised about that

[April 12, 2012, 04:05:02 PM] Lockem Shoto: I wanted if it'll collapse

[April 12, 2012, 04:05:09 PM] Lockem Shoto: Or if the admins were just bots playing with ping

[April 15, 2012, 08:46:22 AM] Hazmat: Sup kiddies

[April 26, 2012, 08:47:02 PM] Unpayed_overtime: Back on Xfire. Now If you want to play Minecraft, Make sure you have a legit one and Xfire me.

[May 11, 2012, 10:52:17 PM] lyfe: sup

[May 12, 2012, 11:17:12 AM] Unpayed_overtime: WARNING: May contain Ecchi content.

News:
Gameservers Offline, mostly playing League. Join Vent for chat/anything else!

 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: C++ Program, Solving a Problem..  (Read 504 times)
`[G]enesis
InfuzionGaming.com Founder
Administrator
VIP User
*****

Popularity: 41
Offline Offline

Posts: 2356


carbon14c@yahoo.com carbon14c
View Profile WWW Email
« on: March 11, 2009, 01:22:14 PM »

Here is the objective of this program:

Write a program to randomly generate 10,000 integers into an array. Sort the Values from least, to greatest. Print the Maximum, Minimum, Mean, Median, the First 10 and Last 10 numbers.


here is what i have:

Quote

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>

const int max_list_size = 10000;
double max, min, mean, median;
int count;
void print_greeting(int length);

void generate_list(double list[ ], int &length);

void sort_list( double list[ ], int length);
int find_minimum (double list[ ], int first, int length);
void swap(double &x, double &y);

double calc_results(double list[ ], int &length, double &max, double &min, double &mean, double &median);
void print_results(double list[ ], int &length, double &max, double &min, double &mean, double &median);


int main()
{
   int length = max_list_size;
   double list[max_list_size];

   print_greeting(length);

   generate_list(list, length);

   for(count = 0; count <= length; count++)
      cout << count + 1 << ". " << list[count] << endl;

   sort_list(list, length);

   for(count = 0; count <= length; count++)
      cout << count + 1 << ". " << list[count] << endl;

   calc_results(list, length, max, min, mean, median);
   print_results(list, length, max, min, mean, median);
   
   return 0;
}


void print_greeting(int length)
{
   cout << setiosflags(ios::fixed | ios::showpoint | ios::right);
   cout << setprecision(2);
   cout << endl << "This sample program does the following:" << endl << endl;
   cout << "1. Generates a list of " << length << " random integers.." << endl << "2. sorts the data.";
   cout << endl << "3. Prints the First and Last 10 integers, along with the Max, Min, Mean, and Median.";
   cout << endl << endl;
}

void generate_list(double list[ ], int &length)
{
   
   double data;
   int count, length2;
   length2 = 0;

   for(count = 0; count <= length; count++)
   {
      data = rand();
      list[count] = data;
      cout << "Location " << count << ": " << list[count]  << endl;
   }

}

double calc_results(double list[ ], int &length, double &max, double &min, double &mean, double &median)
{
   double current;
   int count, temp;
   temp = length/2;
   current = 0;

   for(count = 0; count <= length; count++)
      current = current + list [count];
   current = current / length;

   max = list[length];
   min = list[0];
   median = list[temp];
   mean = current;

   return 0;
}

void print_results(double list[ ], int &length, double &max, double &min, double &mean, double &median)
{   
   int count;
   
   cout << "--------------------------" << endl;
   cout << "----------" << endl;
   cout << "Statistics" << endl;
   cout << "----------" << endl;
   cout << "Max Value: " << max << endl;
   cout << "Min Value: " << min << endl;
   cout << "Median Value: " << median << endl;
   cout << "Mean Value: " << mean << endl;
   cout << "--------------------------" << endl;
   cout << "----------" << endl;
   cout << "First 10" << endl;
   cout << "----------" << endl;

   for(count = 0; count <= 9; count++)
      cout << count +1 << ". " << list[count] << endl;

   cout << "--------------------------" << endl;
   cout << "----------" << endl;
   cout << "Last 10" << endl;
   cout << "----------" << endl;

   for(count = length; count >= length - 10; count--)
      cout << count +1 << ". " << list[count] << endl;

   cout << "--------------------------" << endl;

}

//-----------------------
//Sorting Functions Below
//-----------------------

void sort_list(double list[ ], int length)
{
   int min_index = 0;

   for (int j = 0; j < length - 1; ++j)
   {
      min_index = find_minimum(list, j, length);
      if (min_index != j)
         swap (list[j], list[min_index]);
   }

}

int find_minimum (double list[ ], int first, int length)
{
   int min_index = first;

   for (int j = first + 1; j < length; ++j)
      if (list[j] < list[min_index])
         min_index = j;
   return min_index;
}

void swap(double &x, double &y)
{
   double temp = x;
   x = y;
   y = temp;
}





i just cant get it to work.. this is the output i am getting:


Quote
Location 9731: 31523.00
Location 9732: 4413.00
Location 9733: 18098.00
Location 9734: 23546.00
Location 9735: 25687.00
Location 9736: 30478.00
Location 9737: 28212.00
Location 9738: 8918.00
Location 9739: 32482.00
Location 9740: 25684.00
Location 9741: 20714.00
Location 9742: 22356.00
Location 9743: 14019.00
Location 9744: 31458.00
Location 9745: 6127.00
Location 9746: 4730.00
Location 9747: 17401.00
Location 9748: 18826.00
Location 9749: 12892.00
Location 9750: 29766.00
Location 9751: 16024.00
Location 9752: 24544.00
Location 9753: 10755.00
Location 9754: 30102.00
Location 9755: 14619.00
Location 9756: 20288.00
Location 9757: 12384.00
Location 9758: 4421.00
Location 9759: 21756.00
Location 9760: 15635.00
Location 9761: 30668.00
Location 9762: 2199.00
Location 9763: 1410.00
Location 9764: 27213.00
Location 9765: 11508.00
Location 9766: 18135.00
Location 9767: 29915.00
Location 9768: 21562.00
Location 9769: 9354.00
Location 9770: 10302.00
Location 9771: 16234.00
Location 9772: 21115.00
Location 9773: 4410.00
Location 9774: 10263.00
Location 9775: 31074.00
Location 9776: 22092.00
Location 9777: 21984.00
Location 9778: 14881.00
Location 9779: 12948.00
Location 9780: 12040.00
Location 9781: 18424.00
Location 9782: 21301.00
Location 9783: 27781.00
Location 9784: 30702.00
Location 9785: 30293.00
Location 9786: 25896.00
Location 9787: 14020.00
Location 9788: 4901.00
Location 9789: 18455.00
Location 9790: 20979.00
Location 9791: 25978.00
Location 9792: 22014.00
Location 9793: 2007.00
Location 9794: 6673.00
Location 9795: 5699.00
Location 9796: 19066.00
Location 9797: 12068.00
Location 9798: 18985.00
Location 9799: 30524.00
Location 9800: 10764.00
Location 9801: 1587.00
Location 9802: 27629.00
Location 9803: 1118.00
Location 9804: 30111.00
Location 9805: 27602.00
Location 9806: 32322.00
Location 9807: 12042.00
Location 9808: 20187.00
Location 9809: 22942.00
Location 9810: 14497.00
Location 9811: 9188.00
Location 9812: 30372.00
Location 9813: 30838.00
Location 9814: 7613.00
Location 9815: 8933.00
Location 9816: 5348.00
Location 9817: 22831.00
Location 9818: 8550.00
Location 9819: 7596.00
Location 9820: 11659.00
Location 9821: 9609.00
Location 9822: 26029.00
Location 9823: 4117.00
Location 9824: 26324.00
Location 9825: 23906.00
Location 9826: 21321.00
Location 9827: 18189.00
Location 9828: 1225.00
Location 9829: 29738.00
Location 9830: 4409.00
Location 9831: 2469.00
Location 9832: 15881.00
Location 9833: 25239.00
Location 9834: 8107.00
Location 9835: 22629.00
Location 9836: 19913.00
Location 9837: 154.00
Location 9838: 6685.00
Location 9839: 14050.00
Location 9840: 26393.00
Location 9841: 15249.00
Location 9842: 25794.00
Location 9843: 13202.00
Location 9844: 28008.00
Location 9845: 31934.00
Location 9846: 18727.00
Location 9847: 8672.00
Location 9848: 5707.00
Location 9849: 21496.00
Location 9850: 17942.00
Location 9851: 14972.00
Location 9852: 22650.00
Location 9853: 15008.00
Location 9854: 5819.00
Location 9855: 5621.00
Location 9856: 19483.00
Location 9857: 727.00
Location 9858: 13573.00
Location 9859: 13705.00
Location 9860: 24644.00
Location 9861: 18158.00
Location 9862: 23375.00
Location 9863: 3644.00
Location 9864: 2491.00
Location 9865: 27934.00
Location 9866: 18752.00
Location 9867: 18983.00
Location 9868: 20224.00
Location 9869: 4986.00
Location 9870: 20463.00
Location 9871: 13331.00
Location 9872: 21901.00
Location 9873: 30753.00
Location 9874: 26955.00
Location 9875: 326.00
Location 9876: 21596.00
Location 9877: 12728.00
Location 9878: 24251.00
Location 9879: 28061.00
Location 9880: 4010.00
Location 9881: 19480.00
Location 9882: 17153.00
Location 9883: 24027.00
Location 9884: 16891.00
Location 9885: 7491.00
Location 9886: 17252.00
Location 9887: 31809.00
Location 9888: 5726.00
Location 9889: 26269.00
Location 9890: 5134.00
Location 9891: 863.00
Location 9892: 6130.00
Location 9893: 5721.00
Location 9894: 31667.00
Location 9895: 11049.00
Location 9896: 15017.00
Location 9897: 15665.00
Location 9898: 17011.00
Location 9899: 27725.00
Location 9900: 24908.00
Location 9901: 3162.00
Location 9902: 2050.00
Location 9903: 3524.00
Location 9904: 1215.00
Location 9905: 28599.00
Location 9906: 17670.00
Location 9907: 12455.00
Location 9908: 24711.00
Location 9909: 6221.00
Location 9910: 31680.00
Location 9911: 20037.00
Location 9912: 18569.00
Location 9913: 2807.00
Location 9914: 23535.00
Location 9915: 23666.00
Location 9916: 3092.00
Location 9917: 1882.00
Location 9918: 24049.00
Location 9919: 3106.00
Location 9920: 2594.00
Location 9921: 23324.00
Location 9922: 6444.00
Location 9923: 22070.00
Location 9924: 23259.00
Location 9925: 30033.00
Location 9926: 22956.00
Location 9927: 19093.00
Location 9928: 12891.00
Location 9929: 20022.00
Location 9930: 14605.00
Location 9931: 21887.00
Location 9932: 24757.00
Location 9933: 30498.00
Location 9934: 16028.00
Location 9935: 28446.00
Location 9936: 4920.00
Location 9937: 26297.00
Location 9938: 19129.00
Location 9939: 26974.00
Location 9940: 15089.00
Location 9941: 21859.00
Location 9942: 20864.00
Location 9943: 20479.00
Location 9944: 15472.00
Location 9945: 15869.00
Location 9946: 10408.00
Location 9947: 3817.00
Location 9948: 19663.00
Location 9949: 22222.00
Location 9950: 27818.00
Location 9951: 21183.00
Location 9952: 8177.00
Location 9953: 5820.00
Location 9954: 16678.00
Location 9955: 22454.00
Location 9956: 19464.00
Location 9957: 6847.00
Location 9958: 28803.00
Location 9959: 6824.00
Location 9960: 1626.00
Location 9961: 32663.00
Location 9962: 11991.00
Location 9963: 8292.00
Location 9964: 7492.00
Location 9965: 954.00
Location 9966: 1030.00
Location 9967: 18249.00
Location 9968: 21375.00
Location 9969: 1425.00
Location 9970: 8493.00
Location 9971: 22291.00
Location 9972: 162.00
Location 9973: 12771.00
Location 9974: 9537.00
Location 9975: 17140.00
Location 9976: 6888.00
Location 9977: 30354.00
Location 9978: 5363.00
Location 9979: 20967.00
Location 9980: 3636.00
Location 9981: 3461.00
Location 9982: 2519.00
Location 9983: 8513.00
Location 9984: 1106.00
Location 9985: 13285.00
Location 9986: 23750.00
Location 9987: 13704.00
Location 9988: 640.00
Location 9989: 24972.00
Location 9990: 20353.00
Location 9991: 3465.00
Location 9992: 45.00
Location 9993: 5306.00
Location 9994: 31126.00
Location 9995: 27557.00
Location 9996: 23296.00
Location 9997: 1546.00
Location 9998: 31880.00
Location 9999: 18796.00
Location 10000: 19484.00
1. 41.00
1. 41.00
--------------------------
----------
Statistics
----------
Max Value: 41.00
Min Value: 41.00
Median Value: 41.00
Mean Value: 1.#J
--------------------------
----------
First 10
----------
1. 41.00
2. 18467.00
3. 6334.00
4. 26500.00
5. 19169.00
6. 15724.00
7. 11478.00
8. 29358.00
9. 26962.00
10. 24464.00
--------------------------
----------
Last 10
----------
1. 41.00
Press any key to continue


Obviously there are some problems.. the output is way off. And, i keep getting those exact numbers on each run.. According to the textbook the Rand function is supposed to use the Machine's current time as a seed so technically i shouldnt be getting the same values every time i run the program.

I could forget this problem and choose a different one to work on, but i dont want to.. im dying to get it to work.





Do not reply unless you have some useful advice, i will just delete your post
« Last Edit: March 11, 2009, 01:24:04 PM by `[G]enesis » Logged



voice.izgs.info:9987
cs1.izgs.info:27015
cs2.izgs.info:27025
cs3.izgs.info:27005
cod4.izgs.info:28960
hce1.izgs.info:2302
hce2.izgs.info:2301
hce3.izgs.info:2300
heleos
200+ user!
***

Popularity: 9
Offline Offline

Posts: 263


:O

heleoslol@gmail.com hele0s
View Profile Email
« Reply #1 on: March 11, 2009, 01:56:02 PM »

I think I got it working....

Code:
----------
Statistics
----------
Max Value: 89057537.00
Min Value: 100669.00
Median Value: 1065103348.00
Mean Value: 1067592217.27
--------------------------
----------
First 10
----------
1. 100669.00
2. 172621.00
3. 479345.00
4. 871000.00
5. 881759.00
6. 1121937.00
7. 1323524.00
8. 1648609.00
9. 1769972.00
10. 1839949.00
--------------------------
----------
Last 10
----------
10001. 89057537.00
10000. 2147469841.00
9999. 2146830235.00
9998. 2146753918.00
9997. 2146735658.00
9996. 2146690120.00
9995. 2146410762.00
9994. 2146285188.00
9993. 2146220943.00
9992. 2145854098.00
9991. 2145479782.00
--------------------------

Does that look right?

If so.. the problem is your variable declarations. You shouldn't be using global variables here, it screws everything up. Also, i think your counter is a little messed up because it counts from 1 to 1001

You can play around with the rand function so it doesn't print such large numbers, you can set a max value for it. You're right about how it gets its values too, it takes them from the CPU (probably clock cycles) so the numbers arent exactly random
« Last Edit: March 11, 2009, 02:03:14 PM by heleos » Logged

`[G]enesis
InfuzionGaming.com Founder
Administrator
VIP User
*****

Popularity: 41
Offline Offline

Posts: 2356


carbon14c@yahoo.com carbon14c
View Profile WWW Email
« Reply #2 on: March 11, 2009, 03:39:18 PM »

i try to stay away from global variables, so yes you could be right about that.

the counter COULD be messed up, i am dealing with array's and the memory locations for those start at 0 so that might be why.



you should definitly send me the modified source code Grin
Logged



voice.izgs.info:9987
cs1.izgs.info:27015
cs2.izgs.info:27025
cs3.izgs.info:27005
cod4.izgs.info:28960
hce1.izgs.info:2302
hce2.izgs.info:2301
hce3.izgs.info:2300
heleos
200+ user!
***

Popularity: 9
Offline Offline

Posts: 263


:O

heleoslol@gmail.com hele0s
View Profile Email
« Reply #3 on: March 11, 2009, 04:29:11 PM »

This worked for me:

Code:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>

const int max_list_size = 10000;
void print_greeting(int length);

void generate_list(double list[ ], int &length);

void sort_list( double list[ ], int length);
int find_minimum (double list[ ], int first, int length);
void swap(double &x, double &y);

double calc_results(double list[ ], int &length, double &max, double &min, double &mean, double &median);
void print_results(double list[ ], int &length, double &max, double &min, double &mean, double &median);


int main()
{
   int length = max_list_size;
   double list[max_list_size];
   double max, min, mean, media = 0;
   int count = 0;
   print_greeting(length);

   generate_list(list, length);

   for(count = 0; count <= length; count++)
      cout << count + 1 << ". " << list[count] << endl;

   sort_list(list, length);

   for(count = 0; count <= length; count++)
      cout << count + 1 << ". " << list[count] << endl;

   calc_results(list, length, max, min, mean, median);
   print_results(list, length, max, min, mean, median);
   
   return 0;
}


void print_greeting(int length)
{
   cout << setiosflags(ios::fixed | ios::showpoint | ios::right);
   cout << setprecision(2);
   cout << endl << "This sample program does the following:" << endl << endl;
   cout << "1. Generates a list of " << length << " random integers.." << endl << "2. sorts the data.";
   cout << endl << "3. Prints the First and Last 10 integers, along with the Max, Min, Mean, and Median.";
   cout << endl << endl;
}

void generate_list(double list[ ], int &length)
{
   
   double data;
   int count, length2;
   length2 = 0;

   for(count = 0; count <= length; count++)
   {
      data = rand();
      list[count] = data;
      cout << "Location " << count << ": " << list[count]  << endl;
   }

}

double calc_results(double list[ ], int &length, double &max, double &min, double &mean, double &median)
{
   double current;
   int count, temp;
   temp = length/2;
   current = 0;

   for(count = 0; count <= length; count++)
      current = current + list [count];
   current = current / length;

   max = list[length];
   min = list[0];
   median = list[temp];
   mean = current;

   return 0;
}

void print_results(double list[ ], int &length, double &max, double &min, double &mean, double &median)
{   
   int count;
   
   cout << "--------------------------" << endl;
   cout << "----------" << endl;
   cout << "Statistics" << endl;
   cout << "----------" << endl;
   cout << "Max Value: " << max << endl;
   cout << "Min Value: " << min << endl;
   cout << "Median Value: " << median << endl;
   cout << "Mean Value: " << mean << endl;
   cout << "--------------------------" << endl;
   cout << "----------" << endl;
   cout << "First 10" << endl;
   cout << "----------" << endl;

   for(count = 0; count <= 9; count++)
      cout << count +1 << ". " << list[count] << endl;

   cout << "--------------------------" << endl;
   cout << "----------" << endl;
   cout << "Last 10" << endl;
   cout << "----------" << endl;

   for(count = length; count >= length - 10; count--)
      cout << count +1 << ". " << list[count] << endl;

   cout << "--------------------------" << endl;

}

//-----------------------
//Sorting Functions Below
//-----------------------

void sort_list(double list[ ], int length)
{
   int min_index = 0;

   for (int j = 0; j < length - 1; ++j)
   {
      min_index = find_minimum(list, j, length);
      if (min_index != j)
         swap (list[j], list[min_index]);
   }

}

int find_minimum (double list[ ], int first, int length)
{
   int min_index = first;

   for (int j = first + 1; j < length; ++j)
      if (list[j] < list[min_index])
         min_index = j;
   return min_index;
}

void swap(double &x, double &y)
{
   double temp = x;
   x = y;
   y = temp;
}
Logged

`[G]enesis
InfuzionGaming.com Founder
Administrator
VIP User
*****

Popularity: 41
Offline Offline

Posts: 2356


carbon14c@yahoo.com carbon14c
View Profile WWW Email
« Reply #4 on: March 11, 2009, 04:42:00 PM »

you're a lifesaver.


thanks dude Smiley
Logged



voice.izgs.info:9987
cs1.izgs.info:27015
cs2.izgs.info:27025
cs3.izgs.info:27005
cod4.izgs.info:28960
hce1.izgs.info:2302
hce2.izgs.info:2301
hce3.izgs.info:2300
heleos
200+ user!
***

Popularity: 9
Offline Offline

Posts: 263


:O

heleoslol@gmail.com hele0s
View Profile Email
« Reply #5 on: March 11, 2009, 05:12:02 PM »

No problem. Stay away from global variables Tongue
Logged

`[G]enesis
InfuzionGaming.com Founder
Administrator
VIP User
*****

Popularity: 41
Offline Offline

Posts: 2356


carbon14c@yahoo.com carbon14c
View Profile WWW Email
« Reply #6 on: March 11, 2009, 06:45:03 PM »

yeah, i kind of threw that program together very quickly :/

i have a problem with staying on task, i'll start working on one part of the program then get sidetracked and start working on a different part


so i end up with alot of half-done shit haha.. but thanks for the help. it was a pain in the ass getting it to compile and run with Express 08 >_>
Logged



voice.izgs.info:9987
cs1.izgs.info:27015
cs2.izgs.info:27025
cs3.izgs.info:27005
cod4.izgs.info:28960
hce1.izgs.info:2302
hce2.izgs.info:2301
hce3.izgs.info:2300
heleos
200+ user!
***

Popularity: 9
Offline Offline

Posts: 263


:O

heleoslol@gmail.com hele0s
View Profile Email
« Reply #7 on: March 12, 2009, 12:18:28 PM »

Just another reason to code in linux Tongue
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!