C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 1st, 2012, 01:21 PM
KevinFCTwente KevinFCTwente is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 KevinFCTwente User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);
 	 }

}

Reply With Quote
  #2  
Old October 1st, 2012, 01:59 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 44 m 32 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

Reply With Quote
  #3  
Old October 13th, 2012, 07:47 AM
Cornel1923 Cornel1923 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 1 Cornel1923 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 35 sec
Reputation Power: 0
Unhappy 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..

Reply With Quote
  #4  
Old October 13th, 2012, 08:05 AM
hamed.samie hamed.samie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 hamed.samie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Homework

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap