943,070 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11866
  • C++ RSS
Jun 26th, 2007
-3

One Dimensional Array

Expand Post »
One-dimensional arrays in a function
Ten students do a multiple choice assignment consisting of twenty questions. The lecturer wants to display their
names and the mark obtained by each on the screen. Write a program to help him.
Declare two global constants, namely NR_STUDENTS (set equal to 10) and NR_QUESTIONS (set equal to 20). The
program should consist of three functions. No global variables may be used.
The main function:
Declare the following four one-dimensional arrays:
names of type string where the 10 names are to be stored,
correctAnswers of type intstoring the correct answers to the 20 questions,
answers of type int where the 20 answers of one student are to be stored,
marks of type int where the mark of each of the 10 students will be stored.

The array correctAnswers has to be initialised in the declaration statement.
The main function should contain a forloop going from 1 to 10. Inside the loop two functions have to be
called: the void function inputNameAndAnswers to input the name and answers of one student, and the
int function markOfStudentto calculate the mark of one student.
After the loop is exited, the names of all the students and the mark of each should be displayed on the screen.
The voidfunction inputNameAndAnswers:
The name and 20 answers of one student should be input in this function and made available to the main
function. You need not validate the input.
The int function markOfStudent:
The mark out of 20 of one student has to be calculated here and returned to the main function.
Run your program on the data below and submit printouts of the program and output.
Correct answers: 11223344551122334455
Abe 12345123451234512345
Basil 54321543215432154321
Cecelia 11122233344455511122
Doris 54321543215432154321
Elizabeth 11111111111111111111
Freddy 22222222222222222222
Gerty 33333333333333333333
Hercules 44444444444444444444
Irma 55555555555555555555
John 55544433322211155544

Below is my code but it gives errors - please help:

  1. //Calculates the mark of a student
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. const int NR_STUDENTS = 10;
  7. const int NR_QUESTIONS = 20;
  8.  
  9. void inputNameandAnswers( int j)
  10. {
  11. int mark;
  12. string names;
  13. cout << " Enter name: ";
  14. cin >> names[j];
  15.  
  16. for(int k = 0; k <= NR_QUESTIONS; k++)
  17. {
  18. cout << " Enter marks for student: " << names[j] <<endl;
  19. cin >> mark[k];
  20. answers[k] = mark;
  21. }
  22. }
  23. void markofStudent(int correctAnswers[],int answers[])
  24. {
  25. int counter;
  26.  
  27. for(int a = 0; a <=NR_QUESTIONS; a++)
  28. {
  29. if (correctAnswers[a] = answers[a]);
  30. counter ++;
  31. }
  32. return counter;
  33. }
  34.  
  35. int main()
  36. {
  37. string names[NR_QUESTIONS];
  38. int correctAnswers[NR_QUESTIONS];
  39. int answers[NR_QUESTIONS];
  40. int marks[NR_STUDENTS];
  41. int correctAnswers[]{1,1,2,2,3,3,4,4,5,5,1,1,2,2,3,3,4,4,5,5};
  42.  
  43. for( int q = 0; q <= NR_STUDENTS; q++)
  44. {
  45. inputNameandAnswers(q);
  46. marks[q] == markofStudent(r);
  47. }
  48.  
  49. for(int p = 0; p <= NR_STUDENTS; p++)
  50. {
  51. cout << correctAnswers[p];
  52. cout << name[p] << " has got" << marks[p] << " out of 20 "<< endl;
  53. cout << endl ;
  54.  
  55. }
  56. return 0;
  57. }
Please help
Last edited by WaltP; Jun 26th, 2007 at 2:19 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stonecoldstevea is offline Offline
8 posts
since Apr 2007
Jun 26th, 2007
0

Re: One Dimensional Array

C++ Syntax (Toggle Plain Text)
  1. marks[q] == markofStudent(r);

This should be the assignment operator =
the mark of student() function should be int, not void
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Trouble with this function..
Next Thread in C++ Forum Timeline: Microsoft foundation classes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC