The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Homework
Discuss Homework in the C Programming forum on Dev Shed. Homework C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 1st, 2012, 01:21 PM
|
|
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);
}
}
|

October 1st, 2012, 01:59 PM
|
 |
Contributed User
|
|
|
|
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.
|

October 13th, 2012, 07:47 AM
|
|
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.. 
|

October 13th, 2012, 08:05 AM
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|