Need some help...

This is a discussion on Need some help... within the C Programming forums, part of the General Programming Boards category; Can anyone help me fix my C code so that it can accept answers in two decimals for division and ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    Need some help...

    Can anyone help me fix my C code so that it can accept answers in two decimals for division and integers in all other calculation?

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    Here is the code..

    Code:
    /* mymaths.c
     * This program plays a small math game allowing the player to work with +, -, / and *
    */
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main()
    {
     int c, v4;
     double v1, v2, v3; 
     
     {
     srand((unsigned)time (NULL));
     
     do 
     {
     		printf("This program plays a small math game allowing the player to work with addition, subtraction, division and multiplication\n");
     		printf("\n");
    		printf("(For division, please answer in two decimals)\n");
    		printf("\n");
    		printf("Choose one of the following options:\n");
     		printf("1: Play with addition\n");
     		printf("2: Play with subtraction\n");
     		printf("3: Play with division\n");
     		printf("4: Play with multiplication\n");
     		printf("5: Quit program\n");
     		printf("Enter option: ");
     		scanf("%d", &c);
    
     if (c==1)
     {
    	v1 = 1.00 + rand() % 10;
    	v2 = 1.00 + rand() % 10;
    	v3 = v1 + v2;
    	
    	printf("The answer for %2.2lf + %2.2lf is ", v1, v2);
    	scanf("%d", v4);
    	fflush(stdin);
    	
    	if (v4 == v3)
    	{
    	 printf("Well done, your correct\n");
    	 printf("\n");
    	}
    	else{
    	 printf("Sorry, your wrong. %2.2f + %2.2f = %.2f\n", v1, v2, v3);
    	 printf("\n");
    	}
      }
     else if (c==2)
      {	   
    	v1 = 1 + rand() % 10;
    	v2 = 1 + rand() % 10;
    	v3 = v1 - v2;
    	
    	printf("The answer for %1.2lf - %1.2lf is ", v1, v2);
    	scanf("%d", &v4);
    	fflush(stdin);
    	
    	if (v4 == v3){
    	 printf("Well done, your correct\n");
    	 printf("\n");
    	}
    	else{
    	 printf("Sorry, your wrong. %2.2lf - %2.2lf = %1.2lf\n", v1, v2, v3);
    	 printf("\n");
    	}
      }
     else if (c==3)
      {
    	v1 = 1 + rand() % 10;
    	v2 = 1 + rand() % 10;
    	v3 = (v1 / v2);
    	
    	printf("The answer for %2.2lf / %2.2lf is ", v1, v2);
    	scanf("%d", &v4);
    	fflush(stdin);
    	
    	if (v4 == v3){
    	 printf("Well done, your correct\n");
    	 printf("\n");
    	}
    	else{
    	 printf("Sorry, your wrong. %2.2lf / %2.2lf = %1.2lf\n", v1, v2, v3);
    	 printf("\n");
    	}
      }
     else if (c==4)
      {
    	v1 = 1 + rand() % 10;
    	v2 = 1 + rand() % 10;
    	v3 = v1 * v2;
    	
    	printf("The answer for %2.2lf * %2.2lf is ", v1, v2);
    	scanf("%d", &v4);
    	fflush(stdin);
    	
    	if (v4 == v3){
    	 printf("Well done, your correct\n");
    	 printf("\n");
    	}
    	else{
    	 printf("Sorry, your wrong. %2.2lf * %2.2lf = %2.2lf\n", v1, v2, v3);
    	 printf("\n");
    	}
      }
     else if (c==5)
     {
     	printf("%d is not an option. Please select again\n", c);
     	return 0;
     }
     }while (c != 5);
     }
     }/* end main*/

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    fflush(stdin);

    Do not flush input streams - read FAQ

    so you know how to read int from user.
    do read double - you will use &#37;lf format of the scanf

    in your comparison - better use only integers like
    Code:
    if (c==1) /* addition */
    {
    	int v1 = 1 + rand() % 10;
    	int v2 = 1 + rand() % 10;
    	int v3 = v1 + v2;
    	int v4;
    
    	printf("The answer for %d + %d is ", v1, v2);
    	fflush(stdout);
    	scanf("%d", &v4);
    	
    	if (v4 == v3)
    	{
    	    printf("Well done, your correct\n\n");
    	}
    	else
    	{
    	    printf("Sorry, your wrong. %d + %d = %d\n\n", v1, v2, v3);
    	}
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    About my code..

    I already solved the problem for the other problems, but I have problems making the program accept two decimal places answer for division..... Any suggestion?

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    I don't understand exactly your final problem.

    Additional suggestion;

    If you need to use such a pattern as,
    Code:
     if (c==1)
        { C-Statements }
     else if (c==2)
        { C-Statements }
     else if (c==3)
        { C-Statements }
     else if (c==4)
        { C-Statements }
     else if (c==5)
        { C-Statements }
     .
     .
     .
     else if (c==n)
        { C-Statements }
     else
        { C-Statements }
    Instead of If-ElseIf, use Switch-Case structure for the sake of efficiency. There is serious differency between If-ElseIf and Switch-Case.
    Last edited by velid; 03-25-2008 at 02:53 PM.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    My code...

    What I mean is that I want the program to be able to accept answers for the division problem in two decimal places and compare it with the correct answer... Any suggestions?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    accept them as float, multiply by 100 and round to int - compare the resulting integers
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    A new code problem...

    This code have some problems in listing down the data entered by the user.. Can Aybody help?

    Code:
    /*----------------------------------------------------------------------------------
     *
     * This program request the user to enter the number of data that will be entered, 
     * then outputs the mean(average) of the data entered and the largest and smallest 
     * data entered.
     */
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    
    int main(void)
    {
    	char o;	   
    	int a[30], n, i, r; 
    	double Total, mean, min, max;
    	max = -90000.00;
    	min = 90000.00;
    	Total = 0;
    	fflush(stdin);
    	do 
     {
    	printf("Welcome to Statistic Program\n");
    	printf("1 Run Statistic Program\n");
    	printf("2 Quit program\n");
    	printf("Please enter your option:  ");
    	scanf("%d", &o );
     if (o==1)
     {
    	printf("How many numbers?  ");
    	scanf("%d", &n);
    	if((n<2) || (n>30))
    	{
    		printf("Too many numbers or invalid numbers entered\n");
    		printf("\n");
    		return main();
    	}
    	printf("Enter the data one by one\n");
    	printf("n    Data\n");
    	for(i=0; i< n; i++)
    	{
    		 for(r = 0; r < n; r++)
    		 {
    			printf("%d \t", r);
    			scanf("\t %d", &a[i]);
    		 }
    	}
    
    	for(i=0; i<n; i++)
    	{
    		if(a[i]> max)
    		{
    			max = a[i];
    		}	   	   
    	}
    	for(i=0; i<n; i++)
    	{
    		if(a[i]< min)
    		{
    			min = a[i];
    		}
    	  	
    	}
    	for(i=0; i<n; i++)
    	{
    		Total= Total + a[i];
    		mean = Total/n;
    	}
    	
    	printf("\nMean of the values is %6.2f\n", mean);
    	printf("Maximum value is %6.2f\n", max);
    	printf("Minimum value is %6.2f\n", min);
    	printf("\n");
      }
     else if (o==2)
      {	   
    		/*end Program*/
    		printf("Invalid option selected\n");
    		return 0;
      }
     }while (o != 2);
     }/* end main*/

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    fflush(stdin); - it is undefined - read FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    scanf("&#37;d", &o );
    o should be int, not char for %d format - fix it to avoid problems

    return main(); - never call main

    Code:
    for(r = 0; r < n; r++)
    		 {
    			printf("%d \t", r);
    			scanf("\t %d", &a[i]);
    		 }
    you scan same number n times. Why? You think computer cannot understand you first time you enter that number?

    mean = Total/n; - this could be done outside the loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    About my code....

    I want the program to list down the data entered as it is entered by the user.... Any suggestions?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps store the input in an array and then print the content of the array when you want it listed?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    New problem...

    Can anyone give an example on how to make a code for this:

    two measurable quantities x and y will vary as
    y=2x+3
    Write a program that estimates whether a given set of five (x, y) pairs (representing data observed by the self-monitoring machine) read from a data file fits the “ok” line y=2x+3 closely enough. Generate 21 points evenly spread on the line, using x between 0 and 20. Then combine the field data read from the file with these generated points and compute the correlation coefficient for the entire data set. If the correlation coefficient falls below 0.98, issue a “possible chip failure” message. Otherwise, issue an “ok” message.
    Note: three sample data files are provided for testing purpose.
    where the three data files have 5 point each for x-axis and y-axis.
    Please help

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    Can anyone give an example on how to make a code for this:
    Give us the exsample of what you have, we will help to fix errors
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    30
    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int readData(double dataX[], double dataY[]);
    int main(void)
    {
    	double dataX[50], dataY[50];
    	char FILENAME;
    	
    	printf("This program performs the self monitoring of a complex machine.\n");
    	printf("\n");
    	printf("\t Enter diagnosis data file name: ");
    	printf("\t ");
    	printf("\t Enter the slope of the OK line: ");
    	printf("\t Enter the y-intercept of the OK line: ");
    	printf("\t Enter lower bound of x value: ");
    	printf("\t Enter upper bound of x value: ");
    	printf("\t Enter the number of points to be generated: ");
    	return 0;
    }
    
    int readData(double dataX[], double dataY[])
    {
        FILE *dataFile;
        int num_read = 0; /* The number of entries read from file */
       
        /* Open input file.  */
        dataFile = fopen(FILENAME,"r");
       
        /* Check if successful */
        if (dataFile == NULL)
        {
            printf("Cannot open marks file for reading\n");
            exit(1);
        }
    
        /*  Read data .  */
        while (fscanf(dataFile, "%lf", &marks[num_read]) == 1)
            num_read++;
    		
            
        return num_read;
    }

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    Another problem....

    Is there any reason why this code won't open and print the data in the file?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main()
    {
    	char *rdata;
    	int j, k, n = 0; /* The number of entries read from file */
    	double arrayx[6], arrayy[6];
    	FILE *data;
    	printf("Name of file: ");
    	scanf("%c", &rdata);
    	data = fopen(rdata,"r");
        /* Check if successful */
        if (data == NULL)
        {
            printf("Cannot open %c file for reading\n", &rdata);
            exit(1);
        }
    
        /*  Read data .  */
        while (fscanf(data, "%lf", &n) == 1)
            n++;
    	printf("n      X      Y\n");
    	
    	for (j=0; j<n; j++)
    	{
    			printf("%d \t",j);
    			printf("\t %lf \t %lf", &arrayx[j], &arrayy[j]);
    	}
    	return 0;
    }
    The file contents:
    1.5 6.2
    1.7 6.5
    9.3 21.6
    5.9 14.7
    16.1 35.0

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21