problems w/ program involving structures and arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 1
Reputation: hkb is an unknown quantity at this point 
Solved Threads: 0
hkb hkb is offline Offline
Newbie Poster

problems w/ program involving structures and arrays

 
0
  #1
Dec 1st, 2004
hi, i'm working on an assignment for school, and i'm having a lot of problems when i try to compile it. one of the errors is about changing the functions into arrays and getting an error about pointers, and another is trying to get the right info to the right place. i know its kind of long, but if you could help me, i'd be very grateful.

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct Student {
  5. char name [51];
  6. int hwScore[6];
  7. int midterm;
  8. int exam;
  9. };
  10.  
  11. void input(Student& student);
  12. double score (Student student);
  13. void display (Student student[], double total[]);
  14. int minIndex (double totalScore[], int size, int startIndex);
  15. void swap(double totalScore[], int i, int j);
  16. void swap2(Student student[], int i, int j);
  17. void sort (double totalScore[], int size, Student student[]);
  18.  
  19. int main(){
  20. double totalScore [100];
  21. char again;
  22. Student student[100];
  23. int i = 0;
  24.  
  25. do
  26. {
  27. input (student[i]);
  28. totalScore[i] = score(student[i]);
  29. cout << student[i].name << "'s final score is : ";
  30. cout << totalScore[i] << endl;
  31. cout << "Enter more students? (Y or N): ";
  32. cin >> again;
  33. cout << endl;
  34. i++;
  35. cin.ignore (1, '/n');
  36. }while (again == 'y' || again == 'Y');
  37.  
  38. sort(totalScore[], 100, student[]);
  39. for (int i = 0; i < 100; i++)
  40. display(student, totalScore[i]);
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. return 0;
  49. }
  50.  
  51.  
  52. void input (Student& student)
  53. {
  54. cout << "Enter student name: ";
  55. cin.getline (student.name, 51);
  56. cout << endl;
  57.  
  58. for (int i=0; i < 6; i++)
  59. {
  60. int hwNum = i;
  61. cout << "Enter hw " << hwNum + 1 << "(max 10): ";
  62. cin >> student.hwScore[i];
  63.  
  64. while ((student.hwScore[i] > 10) || (student.hwScore[i] < 0))
  65. {
  66. cout << "Invalid input, please enter a number from 0 to 10:" << endl;
  67. cin >> student.hwScore[i];
  68. }
  69. }
  70.  
  71. cout << "Enter midterm score(max 40): ";
  72. cin >> student.midterm;
  73. while ((student.midterm > 40) || (student.midterm < 0))
  74. {
  75. cout << "Invalid input, please enter a number from 0 to 40:" << endl;
  76. cin >> student.midterm;
  77. }
  78. cout << "Enter final score(max 100): ";
  79. cin >> student.exam;
  80. while ((student.exam > 100) || (student.exam < 0))
  81. {
  82. cout << "Invalid input, please enter a number from 0 to 10:" << endl;
  83. cin >> student.exam;
  84. }
  85. }
  86.  
  87. double score (Student student)
  88. {
  89. double totalScore = 0;
  90. double totalHw =0;
  91. for (int i=0; i < 6; i++)
  92. totalHw += student.hwScore[i];
  93.  
  94. totalScore =((totalHw/60 * 30) + (student.midterm/40 * 25) + (student.exam/100 * 45));
  95. return totalScore;
  96. }
  97.  
  98. void display (Student student[], double total[])
  99. {
  100. int i;
  101. cout << student[i].name << endl;
  102. cout << total[i];
  103. }
  104.  
  105. int minIndex (double totalScore[], int size, int startIndex)
  106. {
  107. double min = totalScore[startIndex];
  108. int minInd = startIndex;
  109. for (int i = startIndex + 1; i < size; i++)
  110. {
  111. if (min > totalScore[i]){
  112. minInd = i;
  113. min = totalScore[i];
  114. }
  115. }
  116. return minInd;
  117. }
  118.  
  119. void swap(double totalScore[], int i, int j)
  120. {
  121. double temp = totalScore[i];
  122. totalScore[i] = totalScore[j];
  123. totalScore[j] = temp;
  124. }
  125.  
  126. void swap2(Student student[], int i, int j)
  127. {
  128. Student temp = student[i];
  129. student[i] = student[j];
  130. student[j] = temp;
  131. }
  132.  
  133.  
  134. void sort (double totalScore[], int size, Student student[])
  135. {
  136. for (int i = 0; i < size; i++)
  137. {
  138. int minInd = minIndex (totalScore, size, i);
  139. swap (totalScore[i], totalScore[minInd]);
  140. swap2 (student[i], student[minInd]);
  141. }
  142. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: problems w/ program involving structures and arrays

 
0
  #2
Dec 1st, 2004
	sort(totalScore[], 100, student[]);
Remove the empty brackets.

display(student, totalScore[i]);
The prototype calls for an array, you are passing a single element.

  1. int i;
  2. cout << student[i].name << endl;
Uh, shouldn't i be given a value?

  1. void swap(double totalScore[], int i, int j)
  2. void swap2(Student student[], int i, int j)
  3.  
  4. swap (totalScore[i], totalScore[minInd]);
  5. swap2 (student[i], student[minInd]);
Okay, let's play a game of count the parameters. Then let's play a game of notice the type.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC