Thread: array issue
View Single Post
Old Apr 28th, 2008, 10:48 PM   #1
jimJohnson1
Newbie
 
Join Date: Apr 2008
Posts: 1
Rep Power: 0 jimJohnson1 is on a distinguished road
array issue

2 questions first did the main daniweb site get hacked or something...2nd can someone take a look at my code and see what I am doing wrong...can't seem to pinpoint it

Not sure what I am doing wrong but have an issue with a program...i am to input a file name that i have in my project and it is not doing it right...

This is what my program should look like for each textfile...

Sample Output 1
Enter the file to read in: data1.txt
The numbers in the array are:

45.3 57.4 23.6 34.2
234.6 34.9 54.8 934.9
8432.5 9809.3 539.7 43.9
12.0


The numbers in the array are:

12.0 23.6 34.2 34.9
43.9 45.3 54.8 57.4
234.6 539.7 934.9 8432.5
9809.3


The average of the numbers is 1558.2
The median of the numbers is 54.8

Press any key to continue


What I am getting is...

Enter the file to read in: data1.txt

The numbers in the array are:

The average of the numbers is 45
The median of the numbers is 0

Press any key to continue...

Not sure what I am doing wrong any help would mean alot

c++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6. using namespace std;
  7.  
  8. const int MAX_SIZE = 100;
  9.  
  10. void Fill_Array(int Size[], int& count, int& numbers_used);
  11. void Print_Array(int Size[], int count);
  12. double Calc_Average(int Size[], double average);
  13. void Sort(int Size[], int numbers_used);
  14. void Swap(int& v1, int& v2);
  15. int index_of_smallest(const int Size[], int start_index, int number_used);
  16. int Calc_Median(int median, int Size[]);
  17. void Print_Array_and_Calculations(int median, double average);
  18.  
  19. int main()
  20. {
  21. ifstream in_size;
  22. int count;
  23. int Size [MAX_SIZE],
  24. numbers_used = 0,
  25. median = 0;
  26. double average = 0.0;
  27.  
  28. Fill_Array(Size, count, numbers_used);
  29. Print_Array(Size, count);
  30. average = Calc_Average(Size, average);
  31. Sort(Size, numbers_used);
  32. Calc_Median(median, Size);
  33. Print_Array_and_Calculations(median, average);
  34.  
  35. in_size.close();
  36. return 0;
  37. }
  38.  
  39. void Fill_Array(int Size[], int& count, int& numbers_used)
  40. {
  41. int size;
  42. ifstream in_size;
  43. string text_file;
  44.  
  45.  
  46. cout << "Enter the file to read in: ";
  47. getline(cin, text_file);
  48.  
  49. cout << endl << "The numbers in the array are:" << endl << endl;
  50. in_size.open (text_file.c_str ());
  51. if(in_size.fail())
  52. {
  53. cerr << "Error opening file" << endl;
  54. exit(1);
  55. }
  56. count = 0;
  57. in_size >> size;
  58. while((!in_size.eof()) && (count <= MAX_SIZE))
  59. {
  60. Size[count] = size;
  61. count++;
  62. in_size >> size;
  63. }
  64. in_size.close();
  65. }
  66.  
  67. void Print_Array(int Size[], int count)
  68. {
  69. int number_used = 0;
  70. for(int index = 0; index < number_used; index++)
  71. cout << Size[index] << " ";
  72. }
  73.  
  74. double Calc_Average(int Size[], double average)
  75. {
  76. int total = 0;
  77. for (int i = 0; i < MAX_SIZE; i++)
  78. {
  79. total = total + Size[i];
  80. }
  81. average = double(total) / MAX_SIZE;
  82.  
  83. return average;
  84. }
  85.  
  86. void Sort(int Size[], int number_used)
  87. {
  88. int index_of_next_smallest;
  89.  
  90. for (int index = 0; index < number_used - 1; index++)
  91. {
  92. index_of_next_smallest = index_of_smallest(Size, index, number_used);
  93. Swap(Size[index], Size[index_of_next_smallest]);
  94. }
  95. }
  96.  
  97. void Swap(int& v1, int& v2)
  98. {
  99. int temp;
  100. temp = v1;
  101. v1 = v2;
  102. v2 = temp;
  103. }
  104.  
  105. int index_of_smallest(const int Size[], int start_index, int number_used)
  106. {
  107. int min = Size[start_index],
  108. index_of_min = start_index;
  109. for (int index = start_index + 1; index < number_used; index++)
  110. if(Size[index] < min)
  111. {
  112. min = Size[index];
  113. index_of_min = index;
  114. }
  115. return index_of_min;
  116. }
  117.  
  118. int Calc_Median(int median, int Size[])
  119. {
  120. median = Size [ MAX_SIZE / 2 ];
  121. return median;
  122. }
  123.  
  124. void Print_Array_and_Calculations(int median, double average)
  125. {
  126. cout << endl << "The average of the numbers is " << average;
  127. cout << endl << "The median of the numbers is " << median;
  128. cout << endl << endl;
  129. }
jimJohnson1 is offline   Reply With Quote