Thread: Need help on C programming

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Need help on C programming

    Hey guys can you show me the right direction?
    Code:
    #include "stdio.h"
    #include "math.h"
    
    
    float falsepositives(float accuracy, float incidence, float population)
    {
          return population*(1-incidence)*(1-(accuracy/100));
    }
    void a2question1()
    {
                float a,i,f;
                int p;
                printf("Enter population size: ");
                scanf("%d",&p);
                printf("Enter the accuracy of the test as a percentage (e.g. 99): ");
                scanf("%f",&a);
                printf("Enter incidence of the disease as a decimal fraction (e.g. .0001): ");
                scanf("%f",&i);
                f = falsepositives(a,i,p);
                printf("Population = %d, accuracy = %.2f, incidence = %f,\nfalse positives = %.f\n\n",p,a,i,f);
    }
    float threenumbers(float num1, float num2, float num3)
    {
          num1=num1+num2+num3;
          num2=num1+num2+num3)/3;
          num3=num1*num2*num3;
          return num1,num2,num3;
    }
    void a2question2()
    {
         float a,b,c,s,avg,prod;
         printf("Enter a whole number: ");
         scanf("%f",&a);
         printf("enter a second whole number: ");
         scanf("%f",&b);
         printf("Enter a third whole number: ");
         scanf("%f",&c);
         s = threenumbers(a,b,c);
         avg = threenumbers(a,b,c);
         prod = threenumbers(a,b,c);
         printf("Sum = %f\nAverage = %f\nProduct = %f\n\n",s,avg,prod);
         }
    int main()
    {
        a2question1();
        a2question1();
        a2question1();
        a2question2();
        a2question2();
        a2question2();
        
        
        
                      
        
        
        
        return 0;
    }
    I need the program to add,multiply,find the average, the lowest numbers and the highest numbers all in the float functions, but I can't seem to do it, maybe the teacher post the wrong one? Anyway can you guys show me what direction to go, no need to write the cod

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I don't really understand what your problem is, you need to elaborate more and point out the parts of code you are having issues with.

    I am going to take a guess and say that your problem is mainly with this function
    Code:
    float threenumbers(float num1, float num2, float num3)
    {
          num1=num1+num2+num3;
          num2=num1+num2+num3)/3;
          num3=num1*num2*num3;
          return num1,num2,num3;
    }
    What it looks like to me is that you are trying to somehow store the results of various operations in num1, num2 and num3. And then give the results back to the caller. Unfortunately C does not allow you to return multiple values as you are attempting here. What happens is that your statements are executed (comma is a statement separator with some less-then desirable side-effects in most cases) and then the value of the last statement is returned.

    If you want to store the results in the arguments to the function, you need to pass them as pointers like so:
    Code:
    float num1, num2, num3;
    ...
    void threenumbers(float * num1, float * num2, float * num3)
    {
          *num1=*num1+*num2+*num3;
          *num2=(*num1+*num2+*num3)/3;
          *num3=*num1 * *num2 * *num3;
    }
    ...
    threenumbers(&num1,&num2,&num3);
    ...
    Obviously not the prettiest syntax here but I'm just showing you a raw conversion of your function into one that does (one piece) of what you want. The other problem here is that after the first line of this function *num1 is now containing a different value then what it first had so it basically invalidates the remaining two statements (same problem with *num2 on the third statement). So you need to use temporary variables to store the intermittent operation results. That is the second problem which is a logic problem and I'm going to leave you to try and fix that on your own.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Hi,
    What I am trying to do is to calculate addition,multiplcation and finding the average in one float function which will return back into the main function, I am not sure whether if its possible but that is my assignments.

    Thank you for your answer again, I do not understand the part where up put an asterisk before the num1,num2,num3(*num1,*num2,*num3) for. Can you elaborate?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If it's your assignment, then the chance is that it is possible

    These are pointers. Tutorial on pointers in C.

    I strictly suggest you to read pointers. From the link, from the slides of your course.. From anywhere you can. C and pointers go together If you don't know pointers, then you can not program in C.
    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

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Hi std,
    Thank you for the quick reply, I read through the link you gave and still are quite unsure how will that help. So I know that pointers are used to address "memory," does this means whatever value I gave to x before, does not change?
    As in say I have this function:
    Code:
    int *x
    *x=*x+1
    *x=*x+2
    *x=*x+3
    and if I declare that *x is 5 using scanf, does that means I will get the value of 6,7,8 or do I get *x=x*+1 which is 6, so *x=6+2, then *x=8+3 with the final answer being 11?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I think this example can help.
    Code:
    #include <stdio.h>
    
    /* Read a value. */
    void input(int* v);
    /* Suppose that we want to swap the values */
    /* of two variables. */
    void swapNoPointers(int a, int b);
    void swap(int* a, int* b);
    
    int main(void)
    {
        int var;
        int n = 5;
        
        printf("Please input\n");
        input(&var);
        printf("You tuped: %d\n", var);
        
        printf("swapNoPointers\n");
        printf("var = %d and n = %d\n", var, n);
        swapNoPointers(var, n);
        printf("var = %d and n = %d\n", var, n);
        
        printf("swap\n");
        printf("var = %d and n = %d\n", var, n);
        swap(&var, &n);
        printf("var = %d and n = %d\n", var, n);
        
        return 0;
        
    }
    
    void input(int* v)
    {
        scanf("%d", v);
        /* We don't to pass the reference of the variable,       */
        /* because we have as parameter *v. So, actually we      */
        /* pass as parameter in fscanf function this: &*v, which */
        /* results to this: v                                    */
    }
    
    void swapNoPointers(int a, int b)
    {
        int temp;
        temp = a;
        a = b;
        b = temp;
    }
    
    void swap(int* a, int* b)
    {
        int temp;
        temp = *a;
        *a = *b;
        *b = temp;
    }
    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

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Thanks, that was insightful. Somehow I still can't use it in my function, it will still give me the value for only 1 equation.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by AFCKING View Post
    So I know that pointers are used to address "memory," does this means whatever value I gave to x before, does not change?
    As in say I have this function:
    Code:
    int *x
    *x=*x+1
    *x=*x+2
    *x=*x+3
    To consider your example, first write it in the "normal way":

    Code:
    #include "standard_headers.h"
    #define INIT_VALUE 5
    
    int main(void)
    {
    	int x = INIT_VALUE;
    	printf("Line %d: Now x has the value %d\n", __LINE__, x);
    	x = x + 1;
    	printf("Line %d: Now x has the value %d\n", __LINE__, x);
    	x = x + 2;
    	printf("Line %d: Now x has the value %d\n", __LINE__, x);
    	x = x + 3;
    	printf("Line %d: Now x has the value %d\n", __LINE__, x);
    	
    	return EXIT_SUCCESS;
    }
    And now consider how we can write it using pointers. This normally means we do our own memory management. So it's helpful to define two functions: one to create an object and one to free the object

    Code:
    #include "standard_headers.h"
    #define INIT_VALUE 5
    
    int *new_int(void) {
    	int *x = malloc(sizeof *x);
    	*x = INIT_VALUE;
    	return x;
    }
    
    void delete_int(int *x) {
    	free(x);
    }
    
    int main(void)
    {
    	int *x = new_int();
    	printf("Line %d: Now x has the value %d\n", __LINE__, *x);
    	*x = *x + 1;
    	printf("Line %d: Now x has the value %d\n", __LINE__, *x);
    	*x = *x + 2;
    	printf("Line %d: Now x has the value %d\n", __LINE__, *x);
    	*x = *x + 3;
    	printf("Line %d: Now x has the value %d\n", __LINE__, *x);
    	delete_int(x);
    	
    	return EXIT_SUCCESS;
    }
    Notice that the code is the same. Basically only replacing x by *x

  9. #9
    Registered User
    Join Date
    Jan 2013
    Location
    UK
    Posts
    19
    Change

    Code:
    float threenumbers(floatnum1, floatnum2, floatnum3){
          num1=num1+num2+num3;
          num2=num1+num2+num3)/3;
          num3=num1*num2*num3;
    }
    To

    Code:
    float threenumbers(float num1, float num2, float num3)
    {
          printf("Total Added: %f\n", num1+num2+num3);
          printf("Total Mean: %f\n", (num1+num2+num3)/3);
          printf("Total Multiplied: %f\n ", num1*num2*num3);
    }

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Quote Originally Posted by 0x47617279 View Post
    Change

    Code:
    float threenumbers(floatnum1, floatnum2, floatnum3){
          num1=num1+num2+num3;
          num2=num1+num2+num3)/3;
          num3=num1*num2*num3;
    }
    To

    Code:
    float threenumbers(float num1, float num2, float num3)
    {
          printf("Total Added: %f\n", num1+num2+num3);
          printf("Total Mean: %f\n", (num1+num2+num3)/3);
          printf("Total Multiplied: %f\n ", num1*num2*num3);
    }
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM