Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Need C help

Options
  • 04-03-2005 1:21pm
    #1
    Registered Users Posts: 2,327 ✭✭✭


    My problem is this, in the calc function of my program, it doesn't seem to be putting the values calculated into the variables. No idea why, and in the option function, it seems to plow straight ahead without waiting for the user to select (y/n). Could someone give me a hand?? because I have no idea what the problem is :/
    #include <stdio.h>
    #include <stdlib.h>
    
    void prompt(double *numbers);
    void calc(double *numbers, double *average, double *diff, double *sum);
    void printtable(double *numbers, double *average, double *diff, double *sum);
    void option(void);
    
    void main()
    {
    	double average;
    	double diff;
    	double sum;
    	double numbers[4];
    
    	prompt(&numbers[0]);
    	calc(&numbers[0], &average, &diff, &sum);
    	printtable(&numbers[0], &average, &diff, &sum);
    	option();
    }
    
    void prompt(double *numbers)
    {
    	int i;
    	printf("This program takes in 4 numbers and analyses them\n Numbers must be between 5 and 40 inclusive\n");
    	for (i = 0; i < 4; i++)
    	{
    		printf("Please enter number %d\n", i+1);
    		scanf("%lf", &numbers[i]);
    		if ((numbers[i] > 40)||(numbers[i] < 5))
    		{
    			printf("Invalid number, number must be between 5 and 40 inclusive\n");
    			i = i-1;
    		}
    		else
    		{
    			printf("Number accepted\n");
    		}
    	}
    }
    
    void calc(double *numbers, double *average, double *diff, double *sum)
    {
    	*sum = (numbers[1]-numbers[2])+(numbers[2]-numbers[3])+(numbers[3]-numbers[4]);
    	*average = (numbers[1]+numbers[2]+numbers[3]+numbers[4])/4.0;
    	*diff = ((numbers[1])*(numbers[2]))+((numbers[3])*(numbers[4]));
    }
    
    void option(void)
    {
    	char temp;
    	printf("Would you like to run this program again?(y/n)\n");
    	scanf("%c", &temp);
    	if(temp == 'y')
    	{
    		fflush(stdin);
    		system("cls");
    		main();
    	}
    	else if(temp == 'n')
    	{
    	}
    	else
    	{
    		printf("You have entered a character other than y or n, retry\n");
    		while((temp!= 'y')||(temp!= 'n'));
    		{
    			scanf("%c", &temp);
    			if(temp == 'y')
    			{
    				fflush(stdin);
    				system("cls");
    				main();
    			}
    			else if(temp == 'n')
    			{
    			}
    		}
    	}
    }
    
    void printtable(double *numbers, double *average, double *diff, double *sum)
    {
    	printf("You entered values %.2f, %.2f, %.2f, %.2f\n", numbers[0], numbers[1], numbers[2], numbers[3]);
    	printf("Sum of products = %.2f\n", sum);
    	printf("Average = %.2f\n", average);
    	printf("Sum of differences = %.2f\n", diff);
    }
    


Comments

  • Registered Users Posts: 7,276 ✭✭✭kenmc


    ok well first up there is nothing in numbers[4] and you don't reference numbers[0] in the calc function. also why are you subtracting the nth number from the n-1th number to sum them???

    edit
    also in the print table it needs to be
    printf("Sum of products = %.2f\n", *sum);


  • Registered Users Posts: 2,002 ✭✭✭bringitdown


    printf("Sum of products = %.2f\n", sum);
    printf("Average = %.2f\n", average);
    printf("Sum of differences = %.2f\n", diff);
    
    You passed these to the function as pointers, there is no need to do that as you are not modifying them in the function, pass by value instead.

    In your option function, temp is getting the value \n when you enter y\n

    Hope this points you in the right direction


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,097 Mod ✭✭✭✭Tar.Aldarion


    ok gimme a sec i will do out the whole program my way and you will learn! lol

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



    void prompt();
    void input(int *num1_ptr,int *num2_ptr,int *num3_ptr,int *num4_ptr);
    void calc(int num1, int num2,int num3,int num4,double *average,int *sum,int *product);
    void print(int sum, int product, double average);

    void main ()
    {

    int num1=0,num2=0,num3=0,num4=0;
    int sum=0, product=0;
    double average=0;
    char redo='n';

    do

    {



    prompt();
    input(&num1,&num2,&num3,&num4);
    calc(num1,num2,num3,num4,&average,&sum,&product);
    print(sum,product,average);


    printf("Do you want the program to be run again?(y/n) \n");
    fflush(stdin);
    scanf("%c",&redo);
    system("cls");

    }


    while (redo == 'y' || redo == 'Y');


    }

    void prompt()


    {
    int i;

    for (i=0; i<40; i++)
    {

    printf(">>");
    printf("<<");
    }

    printf("\n");

    printf("Please enter four integers(between 5 and 40) into this program.\nAfter you have entered these values,the sum,average,sum of products and the sum of the differences in the values entered will be shown. \n");


    for (i=0; i<40; i++)
    {
    printf("<<");
    printf(">>");
    }
    printf("\n\n\n\n");

    }

    void input(int *num1_ptr,int *num2_ptr,int *num3_ptr,int *num4_ptr)
    {

    printf("Enter value 1: ");
    scanf("%d",num1_ptr);
    if ((*num1_ptr <5)||(*num1_ptr >40))

    {
    printf("You have entered a wrong value!! \nPlease enter a new one(between 5 and 40,(inclusive):");
    scanf("%d",num1_ptr);



    }


    printf("Enter value 2: ");
    scanf("%d",num2_ptr);
    if ((*num2_ptr <5)||(*num2_ptr >40))

    {

    printf("You have entered a wrong value!! \nPlease enter a new one between 5 and 40,(inclusive):");
    scanf("%d",num2_ptr);



    }

    printf("Enter value 3: ");
    scanf("%d",num3_ptr);
    if ((*num3_ptr <5)||(*num3_ptr >40))

    {

    printf("You have entered a wrong value!! \nPlease enter a new one between 5 and 40,(inclusive):");
    scanf("%d",num3_ptr);


    }

    printf("Enter value 4: ");
    scanf("%d",num4_ptr);
    if ((*num4_ptr <5)||(*num4_ptr >40))

    {

    printf("You have entered a wrong value!! \nPlease enter a new one between 5 and 40,(inclusive):");
    scanf("%d",num4_ptr);

    }

    printf("\n\n\n\n");


    }

    void calc(int num1,int num2,int num3,int num4, double *average, int *sum, int *product)

    {

    *sum = (num1-num2)+(num2-num3)+(num3-num4);
    *average = (num1+num2+num3+num4)/4.0;
    *product = ((num1)*(num2))+((num3)*(num4));
    }

    void print(int sum, int product, double average)
    {

    printf("Sum of the differences is: %d\n",sum);
    printf("Sum of the products is: %d\n",product);
    printf("Average is %.2f\n\n",average);


    }


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    numbers is an array of doubles in the line:

    calc(&numbers[0], ....)

    theres no need to do this, just do,

    calc(numbers, &average, ...)

    C compiler will see array as a static pointer and will by default point to the first element in the array any way, so there's no real need to do this. No harm though, if it helps you understand it a bit better, I suppose.


  • Registered Users Posts: 2,717 ✭✭✭ARGINITE


    This your problem
    printf("Please enter number %d\n",i+1);
    The i+1 put the first value into the second element of the array.
    scanf("%lf",&numbers);
    causing this to point at the second element in the array.


  • Advertisement
  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    kenmc wrote:
    ok well first up there is nothing in numbers[4] and you don't reference numbers[0] in the calc function. also why are you subtracting the nth number from the n-1th number to sum them???
    *hits self for forgetting that first place in an array is at 0*

    the "sum" is the sum of the differences *(yeah i know... i picked a rather bad name for that).

    Thanks for the help kenmc,bringitdown , I got the calc bit fixed up now... still haven't figured out why it keeps plowing through the option :/


  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    ARGINITE wrote:
    This your problem

    The i+1 put the first value into the second element of the array.


    causing this to point at the second element in the array.


    hmm? the i+1 is just so that the user knows which number he's entering (0 in the array being the first number.. hence i+1)
    the value of "i" isn't changed at all.


  • Registered Users Posts: 593 ✭✭✭Cathy


    #include <stdio.h>
    #include <stdlib.h>
    
    void prompt(double *numbers);
    void calc(double *numbers, double *average, double *diff, double *sum);
    void printtable(double *numbers, double *average, double *diff, double *sum);
    void option(void);
    
    void main()
    {
    	double average;
    	double diff;
    	double sum;
    	double numbers[4];
                 [U]char answer;[/U]
    
    	[U]do[/U]
                 { 
                 prompt(&numbers[0]);
    	calc(&numbers[0], &average, &diff, &sum);
    	printtable(&numbers[0], &average, &diff, &sum);
    	[U]answer=option();[/U]
    [U]          }while(answer=='y');[/U]
      
             
    }
    
    void prompt(double *numbers)
    {
    	int i;
    	printf("This program takes in 4 numbers and analyses them\n Numbers must be between 5 and 40 inclusive\n");
    	for (i = 0; i < 4; i++)
    	{
    		printf("Please enter number %d\n", i+1);
    		scanf("%lf", &numbers[i]);
    		if ((numbers[i] > 40)||(numbers[i] < 5))
    		{
    			printf("Invalid number, number must be between 5 and 40 inclusive\n");
    			i = i-1;
    		}
    		else
    		{
    			printf("Number accepted\n");
    		}
    	}
    }
    
    void calc(double *numbers, double *average, double *diff, double *sum)
    {
    	*sum = (numbers[1]-numbers[2])+(numbers[2]-numbers[3])+(numbers[3]-numbers[4]);
    	*average = (numbers[1]+numbers[2]+numbers[3]+numbers[4])/4.0;
    	*diff = ((numbers[1])*(numbers[2]))+((numbers[3])*(numbers[4]));
    }
    
    void option(void)
    {
    	char temp;
    	
              [U]do
                 {
                 printf("Would you like to run this program again?(y/n)\n");
    	scanf("%c", &dummy);
    	scanf("%c", &temp);
              }while(answer!='y'&&answer!='n');[/U]
                 
    
                 [U]if(temp == 'y')
    	{
    		system("cls");
    	}
    	else if(temp == 'n')
    	{
    	}[/U]
    }
    
    void printtable(double *numbers, double *average, double *diff, double *sum)
    {
    	printf("You entered values %.2f, %.2f, %.2f, %.2f\n", numbers[0], numbers[1], numbers[2], numbers[3]);
    	printf("Sum of products = %.2f\n", sum);
    	printf("Average = %.2f\n", average);
    	printf("Sum of differences = %.2f\n", diff);
    }
    

    I underlined what I changed. I did something similar to this for mine, but I don't have the code in front of me so I'm not positive.
    The mistakes other people have corrected are still there, but the y/n option should work.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,097 Mod ✭✭✭✭Tar.Aldarion


    his program is working now! now to hand up my version :)


  • Registered Users Posts: 3,312 ✭✭✭mr_angry


    NeoSlicerZ wrote:
    *hits self for forgetting that first place in an array is at 0*

    the "sum" is the sum of the differences *(yeah i know... i picked a rather bad name for that).

    Thanks for the help kenmc,bringitdown , I got the calc bit fixed up now... still haven't figured out why it keeps plowing through the option :/
    TBH, you're much better off using a while / do-while in any user input where there is a restriction on the kind of character that can be entered. In the case of your code here, the program will only ask the user to re-enter a value once before continuing regardless. You're much better off having a loop there to wait for a coherent value to be entered, thus idiot-proofing the program a bit.


  • Advertisement
  • Registered Users Posts: 2,327 ✭✭✭NeoSlicerZ


    mr_angry wrote:
    TBH, you're much better off using a while / do-while in any user input where there is a restriction on the kind of character that can be entered. In the case of your code here, the program will only ask the user to re-enter a value once before continuing regardless. You're much better off having a loop there to wait for a coherent value to be entered, thus idiot-proofing the program a bit.
    That's exactly what I did heh :-)


Advertisement