Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am making a greedy algorithm to calculate number of coins in a value in cents stored in a variable. I had it all working out basically, but I had no way of differentiating between singular and plural for the denominations, ( printing "1 quarters" instead of "1 quarter" for example) and the way how I was doing it with sequential if conditions for each denomination, I couldn't do the same for singular or plural differentiation for each coin since it would give me back two quarters and one quarter instead of either or, so I'm trying to use an if else statement but it says that the else statement basically has no if statement to relate to which I don't understand. My compiler also says that the if statement nested in the other one has "no body" for some reason.
Don't mind the scanf, I will change to a better input method once I get the rest figured out.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void)
{
int n;
scanf("%d",&n);
int q = (n/25);
if (n > 24)
{
if (n > 49);
printf("%d quarters\n", q);
else
printf("one quarter\n");
}
return 0;
}
You have bad syntax in your code. if (n > 49); {. This line is wrong, there should not be a semi-colon before your opening bracket. Try this.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
int n;
scanf("%d", & n);
int q = (n / 25);
if (n > 24) {
if (n > 49) {
printf("%d quarters\n", q);
} else {
printf("one quarter\n");
}
}
return 0;
}
Remove the semicolon after:
if (n > 49);
This line:
if (n > 49);
has a semicolon. That semicolon is treated as the body of the true path of the if statement. You must also have gotten a compiler error about the unmatched else as well.
FYI: you will sometimes see an empty expression like this used by people writing loops that have no body.
Search well before asking a question.
See this answer to know about how to code if..else and nested if
if(
condition)
if(condition)
is only allowed nit
if
()
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
So I got this question in my end-sem:
//The value returned by sam(3,0) of the below function is:
#include <stdio.h>
int sam(int n,int a)
{
if(n==0)
return a;
else
{
a+=n;
sam(--n, a);
a+=n;
}
}
I calculate using Tree-method and got the answer as 6. But if I compile and directly print return I get 2.
int main()
{
printf("%ld",sam(3,0));
return 0;
}
OUTPUT: 2
I checked again and again with what is wrong and couldn't understand. The mystery is if I print(a) just before it returns. It returns 6 (what I calculated).
#include <stdio.h>
int sam(int n,int a)
{
if(n==0){
printf("%d",a); //Check the change here
return a;
}
else{
a+=n;
sam(--n,a);
a+=n;
}
}
int main()
{
sam(3,0);
return 0;
}
OUTPUT: 6
If I explain this I get 4 marks. Enough to change my grade
With a bit of formatting assistance, please note that the below function only explicitly returns when n is 0. Otherwise it performs recursion, but uselessly since it never returns any value created by the recursion.
As a result, you find yourself in the realm of undefined behavior.
#include <stdio.h>
int sam(int n, int a) {
if (n == 0)
return a;
else {
a += n;
sam(--n, a);
a += n;
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The task is to calculate how many times a certain digit occurs in the entered sequence of numbers. The number of numbers to be entered and the number to be calculated are set by typing. Ask me if you have got question about code. The problem in finding a match with the number entered in the array.Can you give me hints or instructions, also i think about loop while but i don't know how to realize it please
The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, b, n, c=0, arr[30];
printf("The count of numbers: ");
scanf("%d", &n);
printf("The number what is finding: ");
scanf("%d", &b);
for (i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
for(i=0;i < n;i++)
{
if(arr[i]=b)
{
c++;
printf("%d", c);
}
}
}
You should be compiling your code with at least some basic compilation flags. If you do, you will get a heads up that something is wrong before having to run it to find out. It saves a lot of time in the long run. Consult your compiler's documentation.
For instance, it would point out that your if condition is using an assignment (=) instead of an equality comparison (==). It should be:
if (arr[i] == b)
Also, you probably want to print out the total count at the end of the program - after the loop is finished. So move the printf("%d\n", c); after the loop. (You were also missing a newline which you probably wanted).
Also, scanf has a return value - you should check it. If the user enters invalid integers, you want to catch that and handle it properly.
Finally, since you declare your array to be of size 30, you should add a check that the desired length of the input array is no longer than that -- otherwise, you would get a buffer overflow.
Side note: please use more descriptive variable names. Not doing so often leads to confusion, especially for beginners. A small exception to this is for loop counters, like i in this case -- its perfectly fine to use a single letter. But consider b -- there is no obvious meaning; it should be something like target or to_find. Also, c could be count or total. As for n, perhaps size or length would be more suited.
if(arr[i]=b) it's wrong. x = y is an assignment but you want to do a check. To check if two elements are equal you should write if(arr[i] == b).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Following code returns correctly if I enter ZERO or NEGATIVE value. But, if I enter any positive value it does nothing.
Can someone explain why? My expectation is, it should return factorial of positive number.
#include<stdio.h>
int functionfact(int);
void main()
{
int x,fact;
printf("Input an integer value:\n");
scanf("%d",&x);
if (x<0)
printf("Please enter positive value!!");
else if (x==0)
printf ("The factorial of 0 is 1");
else
{
fact=functionfact(x);
printf("The factorial of %d is %d",x,fact);
}
}
int functionfact(int n)
{
return(n*functionfact(n-1));
}
I've upvoted Kalbi's proposal, but I would write it as:
int functionfact(int n)
{
if (n > 0) {
return n * functionfact(n-1);
} else {
return 1;
}
}
Let's face it: this question is asked by a beginner who is learning about the basics of recursion. Best is to first give a complete understanding of how to deal with such kind of programming (as we all struggled with it before, recursion is not easy), and only afterwards, let's make some typical C oneliners.
As Eric pointed out, there is no way for the function to now when to stop.
It should work if you just change the function to
int functionfact(int n)
{
return n > 0 ? (n*functionfact(n-1)) : 1;
}
Hello Dear!
There is not any termination condition for function functionfact. You can try this...
int functionfact(int n)
{
if(n>1)
return(n*functionfact(n-1));
}
I hope u got your answer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Now im having problems with the new code in terms of compiling. I have two great answers but chux's answer is addressed to rectify my code . So by his/her directions my new code is:
#include <math.h>
#include <conio.h>
int main()
{
int n,i,r;
printf("Enter A Number to know its prime or non prime");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{r==1;
break;
}
}
if(r==1)
printf("%d is a non-prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
But on the output it show as 87 is a prime number. I don't know why. But can someone spot my mistake?
At few problems
Assignment vs. compare
if (r=1) assigns 1 to r, so if (r=1) is always true. Certainly a compare was needed, #Ry
// if (r=1)
if (r == 1)
No early break
OP's code: The value of r depends on the last iteration. Certainly once a factor is found, loop should exit.
for(i=2;i<=n-1;i++) {
if(n%i==0)
// r=1;
{ r = 1; break; }
else
r=0;
}
Incorrect functionality for n == 0,1
All values n < 2 incorrectly report as prime.
Inefficient
Code performs up to n loops. Only need to perform sqrt(n) loops. Tip: Do not use floating point math here for an integer problem.
// for(i=2;i<=n-1;i++)
for(i = 2; i <= n/i; i++)
Alternate
Only peek if you must code.
First off, " ... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C .." I was able to get the code to compile without that library file, so you may wish to consider removing it. As for as the code goes, well here is what I came up with:
#include <math.h>
#include <stdio.h>
int isPrime(int value) {
int i = 2;
for(; i < value; i++) {
if((value % i) == 0) {
return 0;
}
}
return value > 1;
}
int main(void){
int n=0,i=0, r=0;
char * s;
printf("\nPlase enter a number to learn if it is prime:");
scanf("%d",&n);
r = isPrime(n);
printf("\n%d is ", n);
s = (r==0)? " not a prime number" : "a prime number";
puts(s);
return 0;
}
After the user inputs a number, the code checks whether it is prime by calling the function isPrime(), a function that returns an int. isPrime is a simple function that attempts to factor a number.
See here for similar live code that I devised.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#include <stdio.h>
int isPrime(int n){
int ndiv = 0;
int i;
for(i=1;i<=n;i=i+1){
if(n%i == 0){
ndiv = ndiv+1;
}
}
if(ndiv == 2){
return 1;
}
else{
return 0;
}
}
int nextPrime(int n){}
int main(){
int a = isPrime(7);
printf(a);
//printf(isPrime(4));
}
This code gives me a run time error, I think there's a problem here with the way I deal with data types while using a functions and the printf command, but I can't really figure it out. Help!
f in printf stands for "format". You need to supply a format string for printing: printf("%d\n", a)
Your isPrime is inefficient: you do not need to attempt dividing all the way up to the number itself. You could stop once you reach the square root of the number
Moreover, you could exit the loop early once you see that the number is not prime.
Once you fix these errors, your program would start running and producing the output that you expect.
Here is a small example of how to use printf. You can find more format specifiers here.
#include <stdio.h>
int main()
{
int a = 97;
int b = 98;
char hello[6] = "world";
printf("%d\n", a);
printf("%d\n", b);
printf("%s\n", hello);
return 0;
}
It is because your method of printing a variable is wrong. Here's the right one.
int main(){
int a = isPrime(7);
printf("%d",a);
}
I'm no C/C++ expert, but try
printf("%d", a);
%d is a format placeholder expecting an integer number, essentially.
That looks like an interesting isPrime function. Not very efficient at all, but different from what I have seen in the past. You can also loop over all the numbers between 1 and n, and just return false (or 0) if you find any that divise n. Or look up more efficient algorithms.