Thread: Need help with pointers

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    19

    Need help with pointers

    I need help getting the pointers right, I believe i got most of it correct, but I'm missing something and have no idea what or how to fix it.

    here is the problem

    Write a function, named sums(), that has two input parameters; an array, called Input of doubles; and an integer which is the number of values stored in the array. Compute the sum of the positive values in the array and the sum of the negative values. Also count the number of values in each category. Return these four answers through output parameters. Write a main program that reads no more than 10 real numbers and stores them in an array. Stop reading numbers when a 0 is entered. Call the sums() function and print the answers it returns. Also compute and print the average values of the positive and negative sets. Align decimal points on numbers
    SAMPLE INPUT:
    -123.45
    -234.56
    576.1
    -9.345
    675.2
    100
    -10
    1654.45
    765.89
    0 (NOT in computation)

    SAMPLE OUTPUT:
    YourName Program#1 CS110
    Input Read:
    9999.9999
    9999.9999
    ...

    Statistics:
    Desc Number Total: Average:
    Positive 99 99999.9999 9999.9999

    Negative 99 99999.9999 9999.9999
    Overall 99 99999.9999 9999.9999

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    19
    Here is what i wrote

    Code:
    #include <stdio.h>
    
    
    
    
    
    
    void sums(int Input[], int elements,  int *numOfP, int *numOfN, int *PSum, int *NSum, int *Paverage, int *Naverage ); 
    
    
    int main()
    
    
    {
        
        int Input[10];
        int PSum = 0, NSum = 0, userInput; //positive and negative sum
        int numOfP = 0, numOfN = 0; //number of postive and negatives
        int i;
        
        int Paverage ;
        int Naverage ;
    
    
        printf ("Username CS110 Program7\n\n");
        printf("Input Read:\n");
    
    
        for(i = 0; i < 10; i++){
    
    
              scanf( " %lf", &userInput);
              if(userInput == 0){
                  break;
              }
              Input[i] = userInput;
        }
        sums(Input, i, &numOfP, &numOfN, &PSum, &NSum, &Paverage, &Naverage );
    
    
         Paverage= (PSum / numOfP) +.5;
         Naverage= (NSum / numOfN) +.5;
        printf("Statistics:\n");
        printf("Desc        Number        Total:        Average: \n");
        printf("Positive: %8,4f     %8.4f         %8.4f\n", numOfP, PSum, Paverage );
        printf("Negative: %8.4f     %8.4f         %8.4f", numOfN, NSum, Naverage );
       
     
        return 0;
        
    }
     
     
    void sums(double Input[], int elements,  int *numOfP, int *numOfN, int *PSum, int *NSumint, *Paverage, int *Naverage)
    {
         int i;
         for(i = 0; i < elements; i++){
               if(Input[i] > 0){
                           *numOfP += 1;
                           *PSum += Input[i];
               }
               else{
                           *numOfN += 1;
                           *NSum += Input[i];
               }
         }
    }
    Last edited by craff25; 04-25-2013 at 03:32 PM.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What makes you feel this code is not ok? Or should we try to find out?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your function declaration and definition don't match:

    Code:
    // Declaration
    void sums(int Input[], int elements,  int *numOfP, int *numOfN, int *PSum, int *NSum );
    
    //Definition
    void sums(double Input[], int elements,  int *numOfP, int *numOfN, double *PSum, double *NSum)
    Also, remember that 'C' is case sensitive:

    Code:
    int PSum = 0, NSum = 0, userInput; //positive and negative sum
    
    // ...
    
    Paverage= (Psum / numOfP) +.5;
    Naverage= (Nsum / numOfN) +.5;
    Your compiler should be giving you warnings and errors that indicate these mistakes.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    19
    Quote Originally Posted by std10093 View Post
    What makes you feel this code is not ok? Or should we try to find out?
    well if you could that be great, my teaching assistant said something with pointers didnt understand and he left for the weekend.. but i think its pointers? i know the average part of the code isn't working sum and total does it also needs to match the output format i know im pretty close though

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    19
    Quote Originally Posted by Matticus View Post
    Your function declaration and definition don't match:

    Code:
    // Declaration
    void sums(int Input[], int elements,  int *numOfP, int *numOfN, int *PSum, int *NSum );
    
    //Definition
    void sums(double Input[], int elements,  int *numOfP, int *numOfN, double *PSum, double *NSum)
    Also, remember that 'C' is case sensitive:

    Code:
    int PSum = 0, NSum = 0, userInput; //positive and negative sum
    
    // ...
    
    Paverage= (Psum / numOfP) +.5;
    Naverage= (Nsum / numOfN) +.5;
    Your compiler should be giving you warnings and errors that indicate these mistakes.
    thank you fixed the mistakes now when i go to run program and input numbers i can input in one # then when i go to input a second it stops working (gives a not responding) and ends

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, it's confusing when you start a second thread on the same (small) program.

    Second, I have NO idea why you are still working with all those parameters going to sum(), when your assignment specifically says you can only have two parameters and they must be the array of doubles and the int number of data items in the array.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I agree with Adak. Stick to one thread.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  4. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM
  5. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM