# Thread: Need help with pointers

1. ## 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
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. 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");

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];
}
}
}```

3. What makes you feel this code is not ok? Or should we try to find out?

4. 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. Originally Posted by std10093
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. Originally Posted by Matticus
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. 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. I agree with Adak. Stick to one thread.

Bye, Andreas

Popular pages Recent additions