Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Arrays (http://www.go4expert.com/forums/arrays-t17052/)

WildAngel 23Apr2009 08:11

Hello everyone,

I was in hospital and missed all the classes when the prof. lectured about Arrays. Now I'm trying to read the chapter in the book but I'm afraid I don't understand very much. I think if I go ahead and try to do some exercise I'll get to understand the texts better. So here's one exercise in the book that I'm trying to do. It would be a GREAT help if someone can show me how to do such exercise? Thanks very much.

-Some CS retarded-

Write a batch program that analyzes exam scores.


The first line contains a title. The second line holds the number of exams. The remaining lines have ten exam scores per line except the last line, which will have from one to ten exam scores. All exam scores are integers. Your program must work for any title (up to 60 characters) and number of exams (up to 100 integers).

For example:

CS-125 Computer Science I Exam 1 Spring 2008
92 58 84 100 76 93 88 75 70 76
89 60 47 59 82 79 77 67 96 72
82 73


The corresponding output formatted as shown below. Of course, print the values you calculate for the mean rather than question marks. The histogram you print should display a distribution of scores exactly as shown using one star per score. You may assume that no line of output will have more than 50 stars.

CS-125 Computer Science I Exam 1 Spring 2008
Number of exams: 22
Mean: ??.??

Score Exams
----- -----
40-49 *
50-59 **
60-69 **
70-79 ********
80-89 *****
>=90 ****

xpi0t0s 23Apr2009 17:39

Re: Arrays
What don't you understand, and where are you stuck?

An array is basically very simple. Conceptually it's a collection of "boxes" that are all referred to by the same name, plus an index that specifies which box. So a bit like house addresses. If you live on Evergreen Terrace, that's the array name. 742 is the index, as are 740, 738 etc. So a certain well known family might be represented as EvergreenTerrace[742], and their neighdiddlyeighbours are either EvergreenTerrace[740] or EvergreenTerrace[744] (assuming US house numbering works the same way as the UK where one side of the road has all even numbers and the other side all odd numbers).

So going back to C; you can declare a single variable as

int a;
An array with 10 elements is declared in this way:

int arr[10];
and this defines 10 memory locations that are addressed as arr[0], arr[1], arr[2],... arr[9]. Note that arr[10] doesn't exist; you get 10 elements numbered 0..9, because C starts counting things at zero.

The power in this comes from the fact that the number can be replaced with a variable. So this code would be very tedious if we couldn't use arrays:

int arr[10];
some_func(arr); // initialise arr
for (int i=0; i<10; i++)
  printf("%d ",arr[i]);

All times are GMT +5.5. The time now is 21:20.