Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
The following is an attempt to print a number of fibonacci sequence numbers, determined by the user. Uses a user-define function, fibonacci(int a). It is printing the wrong output, not a recursive sequence, but a continually doubling sequence. How can the code be fixed so that it works correctly?
#include <stdio.h>
int fibonacci(int a);
void main()
{
int number, range;
printf("Enter the number of Fibonacci numbers: ");
scanf("%d", &range);
number = fibonacci(range);
printf("%d\n", number);
}
int fibonacci(int a)
{
int num1 = 1;
int num2 = 1;
int position;
if (a == 1)
{
printf("%d", num1);
}
if (a == 2)
{
printf("%d\n", num1);
printf("%d", num2);
}
if (a > 2)
{
for(position = 1; a >= position; position++)
{
printf("%d\n", num1);
num1 = num2;
num2 = num1 + num2;
}
}
}
This prints the following output for all numbers:
1
1
2
4
8
16
...
The desired output is the fibonnaci sequence (each number is the sum of the two previous ones):
1
1
2
3
5
8
13
...
The problem is that within the loop you first overwrite num1 by assigning num2 to it:
num1 = num2;
Then you make num2 be the sum of itself and num1, but you have just made num1 equal to num2, so this line is effectively just multiplying num2 by two (i.e., adding it to itself):
num2 = num1 + num2;
You need to preserve the old value and use that for the sum, e.g., by adding a third, temporary, variable.
(Apart from this, you also have various other problems, but all of them are such that they should produce compiler warnings. If they don't, enable all warnings and/or get a better compiler. Once you get warnings, don't ignore them but research the cause for each of them and fix all warnings before you even run your code.)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need some help with my simple calculator program. It seems to work fine until I use the * symbol for multiplication. When I use the * symbol it comes out to be 99 instead of the ASCII equivalent of 42. The arguments it expects are an integer, operator(+ ,-, *, /) and another integer.
#include <stdio.h>
#include <stdlib.h>
/*
void usage() {
printf("This is a calculator program, just put in to numbers and and operator\n");
printf("Example:\n\t2 + 2\n");
}
*/
int main(int argc, char *argv[]) {
int first_number, second_number;
int symbol;
int sum;
first_number = atoi(argv[1]);
second_number = atoi(argv[3]);
symbol = (int)*argv[2];
printf("symbol varable = %d\n", symbol); // debugging for argv[2]
if (symbol == 43 ) {
sum = first_number + second_number;
printf("sum = %d\n", sum);
}
else if (symbol == 45 ) {
sum = first_number - second_number;
printf("sum = %d\n", sum);
}
else if (symbol == 42) {
sum = first_number * second_number;
printf("sum = %d\n", sum);
}
else if (symbol == 47) {
sum = first_number / second_number;
printf("sum = %d\n", sum);
}
return 0;
}
The problem has nothing to do with your code.
When you run your program with command line parameters like 12 * 12 you are putting a wildcard in the command line and the shell sees the * and replaces it with a list of every filename in the directory - The first program in the directory must start with a lower case c if the symbol comes out with 99.
To make it work escape your command line parameters like 12 '*' 12 or 12 \* 12 or disable filename globbing altogether as shown in this answer: Stop shell wildcard character expansion?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So everything works in the program so far how the teacher wants it too. However, when I enter 0 for num2 it executes the printf like I want it too, but it also executes the printf at the end of the main. Is there anyway to stop it from also executing the one at end int main when I enter 0? I apologize if my code sucks. This is my first year(semester actually) coding and the teacher doesn't really teach us and I've been learning from the book
#include <stdio.h>
//Program with 3 functions 1) Divides 2) Multiplies 3) Calls the other functions
float multiply(float num1, float num2);
float divide(float num1, float num2);
void math_call(float num1,float num2);
int main(void){
float num1;
float num2;
//printf/scanf allows you to choose values for the functions
printf("Please enter 2 numbers:\n");
scanf("%f", &num1);
scanf("%f", &num2);
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
return(0);
}
//Calls function 1 and 2
void math_call(float num1,float num2){
divide(num1,num2);
multiply(num1, num2);
}
//Divides the arguments and doesn't allow to divide by zero
float divide(float num1, float num2){
if(num2 != 0){
float divide;
divide = num1/num2;
return divide;
}
else{
printf("You can't divide by zero. So your answer for division is undefined.\n");
return(0);
}
}
//multiplies the arguments
float multiply(float num1, float num2){
if(num2 != 0){
float multiply;
multiply = num1*num2;
return multiply;
}
else{
printf("If you multiply by zero you will always get zero.\n");
return(0);}
}
This is the result
Please enter 2 numbers:
4
0
If you multiply by zero you will always get zero.
You can't divide by zero. So your answer for division is undefined.
Division = 0.000000 Multiplication = 0.000000
RUN SUCCESSFUL (total time: 1s)
Instead of this in your code
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
Rewrite it as below
// call your functions here and fill variables with results
float divResult = divide(num1, num2);
float mulResult = multiply(num1, num2);
// check if num2 is not zero
if(num2 != 0)
{
// if it isn't it will print your message
printf("Division = %f Multiplication = %f", divResult, mulResult);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing an easy code that takes two numbers I enter and tells me which is the Max value and which is the Min value using 2 separate files for functions. here is my code so far. It scans the number and has the correct Max Min but when I go to print the values a crazy number prints.
#include<stdio.h>
int main(void)
{
double num2, max, min, num1;
printf("enter any space-separated pair of decimal numeric values on the same line: ");
scanf("%le%le", &num1, &num2);
max = ComputeMaximum(num1, num2);
min = ComputeMinimum(num1, num2);
printf("%le", max);
printf("%le", min);
return 0;
}
this is my main program, on seperate files in the same project I have:
double ComputeMinimum(double num1, double num2) {
double result;
result = (num1 < num2) ? num1 : num2;
return result;
}
And
double ComputeMaximum(double num1, double num2) {
double result;
result = (num1 > num2) ? num1 : num2;
return result;
}
I would like to have it print to screen:
ComputeMinimum(?, ?) returned ?
ComputeMaximum(?, ?) returned ? (question marks are the values.)
Code is missing prototypes.
That is all, format is OK, functions are OK.
#include<stdio.h>
// Add these to the same file as main()
// Or better yet, add to another file Compute.h and #include "Compute.h"
// here and in the the separate C file
double ComputeMinimum(double num1, double num2);
double ComputeMaximum(double num1, double num2);
int main(void)
...
// suggest adding check
if (2 != scanf("%le%le", &num1, &num2)) {
puts("Input error");
return -1;
}
I'm trying to solve this problem from the UVA Online Judge: http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2456
The problem statement
A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are
some square numbers. Given two numbers a and b you will have to find out how many square numbers
are there between a and b (inclusive).
The code I've attempted:
#include <stdio.h>
int main()
{
long long int num1, num2, count = 0;
int t, i;
while (1)
{
count = 0;
scanf("%lld%lld", &num1, &num2);
if (num1 == 0 && num2 == 0)
break;
for (; num1 * num1 <= num2; num1 ++)
count++;
printf("%lld\n", count);
}
return 0;
}
The Judge's Response
The Online submission process tells me that my code produces the wrong answer, but I'm not able to figure out why. Can anyone see the error in my code?
The first problem you have is that you misunderstand the problem statement.
You should not calculate the square of the numbers but the square root.
If the square root is an integer then and only then is it a square number.
Another way of getting the correct count is in fact calculating the square but not on the numbers from a to b but from the square root of a to square root of b.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am completely new to C, and was writing a small very simple program. The problem is that the scanf() is taking wrong input:-
#include <stdio.h>
int main(int args, char*argv[])
{
int num1 = scanf("%d",&num1) ;
int num2 =scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
When I give 34 and 23 as input it is outputting:-
Num1 = 1
Num2 = 1
Why is it so?
Don't assign the return value of scanf to num1 and num2. The return value of scanf indicates if the scan is successful, not what you thought it was.
int num1, num2;
scanf("%d", &num1);
scanf("%d", &num2);
This is because scanf returns the number of characters it matches. scanf("%d",&num1) and scanf("%d",&num2) will return 1. You are assigning that number, i.e, 1 to num1 and num2.
Now do it as follows:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1;
int num2;
scanf("%d",&num1) ;
scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
Suggested reading: comp.lang.c FAQ list ยท Question 3.8.
You're assigning the return value of scanf into your values. scanf returns how many characters it matched; you're already passing a reference to the variable you want scanf to store the result in.
Why are you assigning the return value of scanf into your num1 and num2?
My suggestion:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1, num2;
scanf("%d",&num1);
scanf("%d", &num2);
printf("Num1 = %d\n", num1);
printf("Num2 = %d\n", num2);
return 0;
}