One Dimensional Array

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Reply

Join Date: Apr 2007
Posts: 8
Reputation: stonecoldstevea is an unknown quantity at this point 
Solved Threads: 0
stonecoldstevea stonecoldstevea is offline Offline
Newbie Poster

One Dimensional Array

 
0
  #1
Jun 26th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 383
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 38
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz

Re: One Dimensional Array

 
0
  #2
Jun 26th, 2007
  1. marks[q] == markofStudent(r);

This should be the assignment operator =
the mark of student() function should be int, not void
Reply With Quote Quick reply to this message  
Reply

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




Views: 4947 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2010 DaniWeb® LLC