Thread: Pointer arrays help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    7

    Pointer arrays help

    Hi all,

    Just looking a little help with my program. I am trying to create a program that allow you to select how many student marks to enter, after entering that the it ask to enter a coursework mark and then a exam mark. I am trying to work out how to make it find the average of the coursework marks and the average of the exam marks but so far can only get it to do average marks of them both. I have included the program in the attachment. Any help would be greatly appriciated.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Algorithm:

    1) Ask user how many marks to enter.
    2) Create a dynamic array of that size.
    3) Begin reading marks from user for that amount to fill the array.
    4) Add up all of the coursework marks in one variable, and all of the exam marks in one variable.
    5) Calculate the averages by this math formula: average = total / num. Remember that you need to perform floating point division, not integer division.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Hi

    Thanks for that response, could you please show what i need to change in my code to enable that to work. I have it working for entering the amount of students i want and will display the coursework and exam marks but cant get the average sorted out. I would be extremily grateful if you could show me what is wrong or needs added to my code.

    Regards
    Stephen

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    You're probably finding your code is erroring here :
    Code:
      if(!(marks = malloc(2*count*sizeof(int))))
    You need to cast the result to the pointer type.

    ie.
    Code:
    marks = (int*) malloc(...)
    Also, your average calculations are going wrong because you're accessing the wrong elements of your integer array. To diagnose this, try adding a printf() statement :

    Code:
    for (i = 0; i < count; i++)
    {
            total += marks[i+1];
            printf("Adding %d to total...\n", marks[i+1]);
    }
    we are one

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > You need to cast the result to the pointer type.
    It's never necessary in C to cast the result of malloc(). Any error associated with not doing so is probably the result of not #include'ing <stdlib.h>.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    Oh yeah sorry, that's C++ :/

    (edit :- or really old, crappy C compilers like the one we have here!)
    Last edited by wiiire; 04-18-2007 at 09:45 AM.
    we are one

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > scanf("&#37;i", marks+2*i);
    So, to average the coursework, you need to step through the array as
    total += marks[2*i];

    Likewise for exams, it would be
    total += marks[2*i+1];

    Personally, it would make more sense to allocate 2 arrays, one for coursework and one for exams.
    Then there would be no need for magic array indexing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Thank you Salem,

    I tried entering that code that you told me and its working now. Thank you very much for your help. Suppose i wanted the program to print out all the marks how could i modify it to do that?
    i.e coursework 1= 12
    exam 1 = 15
    courswork 2 = 22
    exam = 21
    etc?? by the way i am only a beginner programmer so sorry if i ask silly questions. Would i need to create another function protype for print makrs and make a loop or somethign like that?

    Regards
    Stephen

    Quote Originally Posted by Salem View Post
    > scanf("%i", marks+2*i);
    So, to average the coursework, you need to step through the array as
    total += marks[2*i];

    Likewise for exams, it would be
    total += marks[2*i+1];

    Personally, it would make more sense to allocate 2 arrays, one for coursework and one for exams.
    Then there would be no need for magic array indexing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. pointer arrays!!!
    By condorx in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 08:55 AM
  4. pointer arrays
    By condorx in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 09:04 PM
  5. Replies: 4
    Last Post: 11-05-2001, 02:35 PM