views:

203

answers:

5

Hello everyone,

I would just like a push in the right direction here with my homework assignment. Here is the question:

(1) Write a C function called input which returns void, this function prompts the user for input of two integers followed by a double precision value. This function reads these values from the keyboard and finds the product of the two integers entered. The function uses call by reference to communicate the values of the three values read and the product calculated back to the main program. The main program then prints the three values read and the product calculated. Provide test results for the input: 3 5 23.5. Do not use arrays or global variables in your program.

And here is my code:

#include <stdio.h>
#include <stdlib.h>

void input(int *day, int *month, double *k, double *pro);

int main(void){
    int i,j;
    double k, pro;


    input(&i, &j, &k, &pro);
    printf("%f\n", pro);
    return 0;
}

void input(int *i, int *j, double *k, double *pro){

    int x,y;
    double z; 
    double product;

    scanf("%d", &x);
    scanf("%d", &y);
    scanf("%f", &z);


    *pro += (x * y * z);

} 

I can't figure out how to reference the variables with pointers really, it is just not working out for me.

Any help would be great!

+1  A: 

When reading the numbers in the input function you can make use of the pointers iptr, jptr, kptr and proptr to read the values directly into variables i,j and k declared in the main function as:

void input(int *iptr, int *jptr, double *kptr, double *proptr){

    scanf("%d", iptr); // read directly into i using pointer to i.
    scanf("%d", jptr);
    scanf("%f", kptr);   

    *proptr = ( (*iptr) * (*jptr) ); // compute product and assign to pro.
} 
codaddict
Is it me, or did the question not specify me to do anything with the double value read in?
Chad
@Chad, only to read it in and print it in main.
Brandon Bodnár
@Chad: Thats what I got from the question.
codaddict
A: 

What your program is not doing is setting the values of the input to i, j, and k.

Instead of using x,y and z, use the parameters instead.

DeadHead
+2  A: 

You're almost there, but instead of making new variables x, y, and z, use the pointers you passed:

scanf("%d", i);
scanf("%d", j);
scanf("%f", k);

*pro += ((*i) * (*j) * (*k));
rlbond
Why `+=`? The double has not been initialised, so you'll likely get rubbish.
dreamlax
Well it was in his code like that.
rlbond
+1  A: 
*pro += (x * y * z);

This is going to break horribly. You're adding the product to whatever garbage happens to be in pro beforehand. You want to remove the +, i.e.:

*pro = (x * y * z);
Turtle
+3  A: 

You adding to pro but that is not initialized, you are not passing values back apart from pro. You store values into the addresses of variables passed in. In that case you need to dereference pointers to access/retrieve value, *i, and in your method use the passed addresses directly - then you don't need to take address of them again.

This works - I replaced double with float ... :

#include <stdio.h>
#include <stdlib.h>

void input(int *day, int *month, float *k, float *pro);

int main(void){
    int i,j;
    float k, pro;

    i = j = k = pro = 0;

    input(&i, &j, &k, &pro);
    printf("%f\n", pro);
    printf("%d : %d : %f\n", i,j,k);
    return 0;
}

void input(int *i, int *j, float *k, float *pro){

    scanf("%d", i);
    scanf("%d", j);
    scanf("%f", k);

    printf("%d - %d - %f\n", *i,*j,*k);

    *pro += (*i * *j * *k);
}

Output:

1
2
3.5
1 - 2 - 3.500000
7.000000
1 : 2 : 3.500000
stefanB