I'm having trouble with this little piece of code. The problem is related to the data entry. I can't get the data as they are typed. If I enter the following values:
g1 = 3; q = 3 and term(termo) = 3
I get
g1 = 3, q = 0 and term(termo) = 2686724.
Already tried using getchar() after scanf, and fflush(stadin) before and after data input routine, and also put space between "% f (d)". Nothing works.
I am using DevC ++ and the CodeBlocks and both have the same problem (could be the gnu gcc?).
I don't know what else to do. Is the code, is the scanf() or is my PC that's in trouble?
This try to be a C code. The problem is in termos_pg() function routine. The sentence printf("g1=%.f, q=%.f, termo=%d,\n",g1,q,termo); into calcula_pg() function routine shows what I get into termos_pg() routine. Can anybody help me?
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <math.h>
float ind,gn,g1,val,q;
int termo,gx=0,i;
main()
{
setlocale(LC_ALL,"Portuguese");
char X;
printf ("\nSe houver parâmetro desconhecido, tecle s ou S, caso nao, ENTER.\n");
X=getch();
fflush(stdin);
if(X=='s' || X=='S'){
es_pg();
return (0);}
else{
termos_pg();
return (0);
}
system("PAUSE");
return 0;
}
termos_pg()
{
printf("\nDigite o 1º termo da PG: ");
scanf("%f",&g1);
printf("Digite a razao da PG: ");
scanf("%f",&q);
printf("Digite a quantidade de termos: ");
scanf("%d",&termo);
calcula_pg(g1,q,termo);
}
calcula_pg(g1,q,termo)
{
printf("g1=%.f, q=%.f, termo=%d,\n",g1,q,termo);
printf("\n\tA sequência é: \n");
for(i=0;i<termo;i++)
if(++gx==termo)
printf("\ta%d= %20.f.\n",gx,(g1*powf(q,(termo-(termo-i)))));
else
printf("\ta%d= %20.f;\n",gx,(g1*powf(q,(termo-(termo-i)))));
system("PAUSE");
}
es_pg(){;}
You haven't given any types for your arguments to calcula_pg, which means that the compiler assumes you're passing int-typed variables, not float.
Here's an example program to demonstrate the effect:
#include <stdio.h>
void ints(int x, int y, int z)
{
printf("ints: x == %.f, y == %.f, z == %d\n", x, y, z);
}
void floats(float x, float y, int z)
{
printf("floats: x == %.f, y == %.f, z == %d\n", x, y, z);
}
int main()
{
ints(3.14, 3.14, 5);
floats(3.14, 3.14, 5);
return 0;
}
Here's the output:
ints: x == 0, y == 0, z == 3
floats: x == 3, y == 3, z == 5
Add fflush(stdout) after every printf call before scanf
Related
I'm very new to programming, and I'm doing a simple bot in C, that has a calculator function. I'm having some trouble in this piece of code:
char operation = get_char("Insert the mathematical operation: ");
float x = get_float("X: ");
float y = get_float("Y: ");
if (operation == 43)
{
float result = (x + y);
int rresult = round(result);
printf("X + Y = %i\n", rresult);
string confirmation = get_string("Need to do something more? ");
if (strcmp(confirmation, "Y") == 0)
{
return operation;
}
As you can see, this calculator ask the user for a char (*, /, + or - [its everything defined in the other parts of the code, I will not post it here just to be brief] that defines the math operation and then, after doing the calculation and printing the result, asks the user if he wants to do more calculations. If the answer is "Y" (yes), I want to restart this piece of code, asking for the char and the floats, and doing everything. I want to know the simplest way to do this, without making the code looks bad designed.
Also, I'm using CS50 IDE.
I don't have CS50, therefore, plain C. The construct you might want to use is a do-while loop.
#include <stdio.h>
#include <math.h>
char get_char(char * msg) {char c; printf("%s", msg); scanf(" %c", &c); return c;}
float get_float(char * msg) {float f; printf("%s", msg); scanf(" %f", &f); return f;}
int main() {
char confirmation = 'n';
do {
char operation = get_char("Insert the mathematical operation: ");
float x = get_float("X: ");
float y = get_float("Y: ");
if (operation == '+') {
float result = (x + y);
printf("Result in float: %f\n", result);
int rresult = round(result);
printf("X + Y = %i\n", rresult);
}
confirmation = get_char("Need to do something more [y/n]? ");
} while (confirmation == 'y');
return 0;
}
$ gcc -Wall dowhile.c
$ ./a.out
Insert the mathematical operation: +
X: 0.11
Y: 0.88
Result in float: 0.990000
X + Y = 1
Need to do something more [y/n]? y
Insert the mathematical operation: +
X: 0.1
Y: 0.1
Result in float: 0.200000
X + Y = 0
Need to do something more [y/n]? n
$
So I have an exercise that goes like this:
Code a program that user inputs 2 natural numbers X and Y. The output must print the number of combinations that are recursively computed to generate binary numbers using X 1s and Y 0s.
For example, user inputs 2 and 3. The combinations of 2 and 3 generating binary numbers are:
00011
00101
00110
01001
01010
01100
10001
10010
10100
10. 11000
The program must print "10".
So I've coded the recursion but I cant figure out a way to print the number "10".
Here is my code
#include <stdio.h>
#include <stdlib.h>
int recursion(int a, int z)
{
if(a==0 && z==0)
{
printf(".");
return 1;
}
if(a!=0 && z==0)
return recursion(a-1,z);
if(a==0 && z!=0)
return recursion(a,z-1);
if(a!=0 && z!=0)
return recursion(a-1,z)+recursion(a,z-1);
}
int main()
{
int a,z;
scanf("%d %d", &a, &z);
recursion(a,z);
return 0;
}
This code only prints 10 "." instead of the number of dots that I need. Any thoughts?
#include <stdio.h>
#include <stdlib.h>
int recursion(int a, int z)
{ int d;
if(a==0 && z==0)
{
d++;
return 1;
}
if(a!=0 && z==0)
return recursion(a-1,z);
if(a==0 && z!=0)
return recursion(a,z-1);
if(a!=0 && z!=0)
return recursion(a-1,z)+recursion(a,z-1);
}
int main()
{
int a,z,d;
scanf("%d %d", &a, &z);
d=recursion(a,z);
printf("%d",d);
return 0;
}
Your program already returns its level of recursion, a simple way to print it could be:
int main()
{
int a, z, level, read;
do {
read = scanf("%d %d", &a, &z);
if (read != 2) {
printf("Incorrect input, please try again");
}
} while (read != 2);
level = recursion(a,z);
printf("\n%d\n", level); /* Newline characters added to improve readability */
return 0;
}
So the output would be the dots you are printing on line 9 and the level of recursion (10 in your example).
..........
10
#include<stdio.h>
#include <stdlib.h>
int triArea(x, y) {
int baseTimesHeight = x * y;
int area = baseTimesHeight / 2;
return area;
}
int main() {
int x = 0;
int y = 0;
printf("Enter an integer value for the length: \n");
scanf("%d", &x);
printf("Enter an integer value for the height: \n");
int area1 = triArea(x ,y);
printf("The area of the triangle is: %d\n", area1);
printf("Hello world!\n");
return 0;
}
this is the code that i wrote for a simple calculation (I am VERY new to programming in c and low level languages) however no matter the program if it has a scanf it will not output any other code. Anyone have idea as to why this happens? I'd like both a solution and explanation if possible!:)
You forgot the scanf for y:
...
printf("Enter an integer value for the height: \n");
scanf("%d", &y); // <<<<< you forgot this line
and int triArea(x, y) should be int triArea(int x, int y). Any decent compiler should issue a warning for this.
Bonus: why do you call your variable x, and y? Call them length and height instead. Code readability is very important, and correct naming of variables and functions is very important.
This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 1 year ago.
I wrote this C program using 2 functions, but in this first case grade() is not prompting for the input:
#include <stdio.h>
int motivQuote();
char grade();
void main()
{
int x;
char y, z;
printf("Programmer-defined Function Type 2\n");
x = motivQuote();
printf("Returned value is %d\n\n", x);
printf("Expected grades this semester:\n");
printf("Subject1: ");
y = grade();
printf("\nReturned value is %c\n", y);
}
int motivQuote()
{
int n = 0;
printf("Select quote (1 or 2):");
scanf_s("%d", &n);
if (n == 1)
printf("\n~ Robert Collier ~\n\t\"Success is the sum of small efforts, repeated day in and day out.\"\n");
if (n == 2)
printf("\n~ David Bly ~\n\t\"Striving for success without hard work\n\tis like trying to harvest where you haven’t planted.\"\n");
return n + 2;
}
char grade()
{
char grade = 0;
scanf_s("%c", &grade, 1);
return grade;
}
And here in the second case, grade() prompts for the input only in the second call, but the first one is not working:
#include <stdio.h>
int motivQuote();
char grade();
void main()
{
int x;
char y, z;
printf("Programmer-defined Functions\n");
x= motivQuote();
printf("Returned value is %d\n\n", x);
printf("Expected grades this semester:\n");
printf("Subject1: ");
y= grade();
printf("\nSubject2: ");
z= grade();
printf("Returned values are %c and %c\n", y, z);
}
int motivQuote ()
{
int n = 0;
printf("Select quote (1 or 2):");
scanf_s("%d", &n);
if (n==1)
printf("\n~ Robert Collier ~\n\t\"Success is the sum of small efforts, repeated day in and day out.\"\n");
if (n==2)
printf("\n~ David Bly ~\n\t\"Striving for success without hard work\n\tis like trying to harvest where you haven’t planted.\"\n");
return n+2;
}
char grade()
{
char grade = 0;
scanf_s("%c", &grade,1);
return grade;
}
Can anyone tell me what's the reason please?
Use scanf(" %c",&grade,1); with a space before %c. This will solve the problem.
I am trying to write a program that calculates the length of one side of a triangle with the help of the Pythagoras equation (c²=a²+b²). The user must have the option to choose what side he want to calculate, this is what I have tried:
#include <conio.h>
#include <stdio.h>
#include <math.h>
// Pitagora:
// c=sqrt(pow(a,2)+pow(b,2));
// a=sqrt(pow(c,2)-pow(b,2));
// b=sqrt(pow(c,2)-pow(a,2));
int cateta(int x, int y){
int cat;
printf("Dati marimea lui:");
scanf("%d", &x);
printf("Dati marimea lui:");
scanf("%d", &y);
cat=sqrt(pow(x,2)-pow(y,2));
return cat;
}
int main(){
int a,b,c;
char l;
printf("Ce latura doriti sa aflati?");
printf("\n c : ipotenuza\n a : cateta alaturata\n b : cateta opusa\n");
printf("Introduceti litera laturei respective : ");
scanf("%s", &l);
if (l == a){
a=cateta(c,b);
printf("Marimea catetei alaturate este: %d", a);
}
else if (l == b){
b=cateta(c,a);
printf("Marimea catetei opuse este: %d", b);
}
else {
c=sqrt(pow(a,2)+pow(b,2));
printf("Marimea ipotenuzei este: %d", c);
printf("\n");
}
getch ();
return 0;
}
But, for some reason when I give a value of a to the variable &l the program displays the content of this piece of code: printf("Marimea ipotenuzei este: %d", c); instead of scaning the value of x and y, and terminates. Here is a picture with the result: https://www.dropbox.com/s/wzk3osw1t8729et/Untitled.png?dl=0
you are using %s in scanf() for a character type variable, instead use this
scanf(" %c", &l);
First, u need to change the scanf statement to scanf( "%c",&l);
Now the variable l contains the character entered by user.
Next, during comparison change the if condition to if(l=='a') , if( l=='b') as a and b are character literals.
With this changes, the program should work!
Happy coding!!