1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Arrays

Discussion in 'C' started by WildAngel, Apr 23, 2009.

  1. WildAngel

    WildAngel New Member

    Joined:
    Apr 23, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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-

    [FONT=&quot]Write a batch program that analyzes exam scores.[/FONT]

    [FONT=&quot]Input[/FONT]

    [FONT=&quot]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).[/FONT]

    [FONT=&quot]For example:[/FONT]

    [FONT=&quot]CS-125 Computer Science I – Exam 1 Spring 2008[/FONT]
    [FONT=&quot]22[/FONT]
    [FONT=&quot]92 58 84 100 76 93 88 75 70 76[/FONT]
    [FONT=&quot]89 60 47 59 82 79 77 67 96 72[/FONT]
    [FONT=&quot]82 73[/FONT]

    [FONT=&quot]Output[/FONT]

    [FONT=&quot]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.[/FONT]

    [FONT=&quot]CS-125 Computer Science I – Exam 1 Spring 2008[/FONT]
    [FONT=&quot]-----------------------------------[/FONT]
    [FONT=&quot]Number of exams: 22 [/FONT]
    [FONT=&quot]Mean: ??.??[/FONT]

    [FONT=&quot]Score Exams[/FONT]
    [FONT=&quot]----- -----[/FONT]
    [FONT=&quot] <10 [/FONT]
    [FONT=&quot]10-19 [/FONT]
    [FONT=&quot]20-29 [/FONT]
    [FONT=&quot]30-39[/FONT]
    [FONT=&quot]40-49 *[/FONT]
    [FONT=&quot]50-59 **[/FONT]
    [FONT=&quot]60-69 **[/FONT]
    [FONT=&quot]70-79 ********[/FONT]
    [FONT=&quot]80-89 *****[/FONT]
    [FONT=&quot] >=90 ****[/FONT]
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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
    Code:
    int a;
    
    An array with 10 elements is declared in this way:
    Code:
    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:
    Code:
    int arr[10];
    some_func(arr); // initialise arr
    for (int i=0; i<10; i++)
      printf("%d ",arr[i]);
    
     

Share This Page