Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
October 1st, 2012, 01:21 PM
 KevinFCTwente
Registered User

Join Date: Sep 2012
Posts: 2
Time spent in forums: 1 h 20 m 42 sec
Reputation 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
October 1st, 2012, 01:59 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 3,838
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 2 sec
Reputation Power: 1774
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

#3
October 13th, 2012, 07:47 AM
 Cornel1923
Registered User

Join Date: Oct 2012
Posts: 1
Time spent in forums: 34 m 35 sec
Reputation Power: 0
a program for cryptography

Hello..
i really need a little help ...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..

#4
October 13th, 2012, 08:05 AM
 hamed.samie
Registered User

Join Date: Oct 2012
Posts: 6
Time spent in forums: 1 h 19 m 18 sec
Reputation 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

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Homework