Thread: Homework

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Homework


    My program won't work yet so I hope someone here can tell me what i've done wrong. This is my subject

    Write a program that determines real roots x1 and x2 of the equation calculates, using the quadratic formula:
    Reading a, b ​​and c in. Provide appropriate text for input and output. Provide an error if b2-4ac is negative.

    I hope you can correct me
    Code:
    #include <math.h>
     #include <stdio.h>
      
      int main()
      
     {	 	 	 	 	 	 
    	/* variables */
    
      float A, B, C, D, x_1, x_2;
      
    	/* input */
    	
    	/* Values for A, B and C  */
    	   	  	  
    printf("give value for A \n");	  	     	    
    scanf("%f", A );
      
    printf("give value for B \n");
    scanf("%f", B );
      
    printf("give value forC \n");
    scanf("%f", C );
      	
    /* Calculate x_1 and x_2 */
    		
    D = B - 4 * A * C;
       	   	   	   
      if (D>=0)
    	  {
    	  x_1 = -B + sqrtf(D);	
    	  x_2 = -B - sqrtf(D);
    		
    		
    /* output */
    	
    	printf(" x_1 \n");  	
    	printf(" x_2 \n");
    	
    	return(0);
    	  }
      else
     	  {
     	printf ("D can't be negative \n");
    		
    	return(0);
     	 }
    
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    A couple of points.
    1. Use a compiler with good diagnostics.
    $ gcc -Wall foo.c -lm
    foo.c: In function ‘main’:
    foo.c:16:1: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:19:1: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:22:1: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat]
    foo.c:9:26: warning: variable ‘x_2’ set but not used [-Wunused-but-set-variable]
    foo.c:9:21: warning: variable ‘x_1’ set but not used [-Wunused-but-set-variable]
    foo.c:16:6: warning: ‘A’ is used uninitialized in this function [-Wuninitialized]
    foo.c:19:6: warning: ‘B’ is used uninitialized in this function [-Wuninitialized]
    foo.c:22:6: warning: ‘C’ is used uninitialized in this function [-Wuninitialized]

    The first 3 are telling you that you forgot the &A etc when calling scanf.

    Next, good indentation will make it easier to follow your code, and also make it easier for others to understand (and help).
    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main()
    {
      /* variables */
      float A, B, C, D, x_1, x_2;
    
      /* input */
      /* Values for A, B and C  */
      printf("give value for A \n");
      scanf("%f", A);
    
      printf("give value for B \n");
      scanf("%f", B);
    
      printf("give value forC \n");
      scanf("%f", C);
    
      /* Calculate x_1 and x_2 */
      D = B - 4 * A * C;
    
      if (D >= 0) {
        x_1 = -B + sqrtf(D);
        x_2 = -B - sqrtf(D);
    
        /* output */
        printf(" x_1 \n");
        printf(" x_2 \n");
        return (0);
      } else {
        printf("D can't be negative \n");
        return (0);
      }
    
    }
    There are a couple of other mistakes as well, but that should at least get you running.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Unhappy a program for cryptography


    Hello..
    i really need a little help :D...i want to do a program that make cryptography substitution...i know what i have to do ,but i am a little weak at programming....ok..my problem is when i want to copy a file in another file(this is simple) ,but in the 2nd file i want to have only the characters ..whitout spaces or comma or dots....just characters...forward i think i know what i have to do ,but here is my problem...this is what i did.:
    #include<stdio.h>
    int main(){
    FILE *p,*q;
    char file1[20],file2[20];
    char ch;
    printf("\nEnter the source file name to be copied:");
    gets(file1);
    p=fopen(file1,"r");
    if(p==NULL){
    printf("cannot open %s",file1);
    exit(0);
    }
    printf("\nEnter the destination file name:");
    gets(file2);
    q=fopen(file2,"w");
    if(q==NULL){
    printf("cannot open %s",file2);
    exit(0);
    }
    while((ch=getc(p))!=EOF)
    putc(ch,q);
    printf("\nCOMPLETED");
    fclose(p);
    fclose(q);

    char *txt;
    txt=&ch;
    int i,count=1;

    for(i=0;i<=count;i++);
    printf("\n\nSunt %d de caractere \n\n",txt);

    getch();

    }


    ...i don't know how to copy only the characters ,..and ye...how to convert them..from uppercase to lowercase but this i can do to the second file..thx.. :D
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep Power
    0

    myfopen()


    Hello every body,

    I want to write a function myfopen()

    int* myfopen(const char*, const char)

    that works like fopen() but i must write this function without using <STDIO.h>


    I dont have any idea, Can you plz help me?

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo