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
In this factorial recursion "printf" outputs 2 6 8, Can't understand why...
#include <stdio.h>
int f(int n)
{
int k;
if (n==1)
return 1;
else
k = n*f(n-1);
printf("%d ",k);
}
int main()
{
f(4);
}
The original version of the code in the question was:
int f(int n) {
int k;
if (n == 1)
return 1;
else
k = n * f(n - 1);
printf("%d ", k);
}
int main() {
f(4);
}
This code has undefined behavior because you do not return a value properly if n != 1, causing the calling code to use an unpredictable value in its own calculation. The behavior is undefined, anything can happen.
Adding the return statement fixes this issue. Note however these extra points:
variable k is useless in the f function.
f should return 1 for an argument of 0.
you should output a newline after the number and return 0 in main.
Here is modified version:
#include <stdio.h>
int f(int n) {
if (n <= 1)
return 1;
else
return n * f(n - 1);
}
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("%d! = %d\n", i, f(i));
}
return 0;
}
There are many mistakes you are making here:
printf("%d ",k);
this line is never going to be executed because in any case if() else clause will return before it.
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 2 years ago.
Improve this question
This is a c code for getting the average value of the addition of array components.
But once I run this which is not outputting anything.
Can anyone help me out where I got the code wrong?
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
double solution(int arr[], size_t arr_len);
int main()
{
int array[10] = { 1,2,3,4,5,6,7,8,9,10 };
int length = sizeof(array[10]);
double out = solution(array, length);
printf("solution is %f\n", out);
return 0;
}
double solution(int arr[], size_t arr_len) {
double answer = 0;
int total = 0;
for (int i = 0; i < arr_len;){
total += arr[i];
}
answer = total / arr_len;
return answer;
}
You are not incrementing the loop counter in solution so its stuck in an infinite loop.
for (int i = 0; i < arr_len;){
needs to be
for (int i = 0; i < arr_len; i++) {
Edit:
sizeof is also wrong. It returns the total memory used by the array. So you need to do
int length = sizeof(array) / sizeof(array[0])
which divides the total memory by the size of one element to give you the total number of elements.
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
I want to create a program who store the rest of successive division in the array.But unfortunately the array i created refuse store the remain correctly for example 5/2 the remain is suppose to be 1 but the array store another value
#include <stdio.h>
#include <stdlib.h>
int divise(int n){
int i=0;
int remain[20];
int rest;
while(n!=0){
rest = n%2;
remain[i] = rest;
n = n/2;
i++;
printf("%d\n",remain[i]);
}
}
int main(){
divise(10);
}
The mistake is with your i++ statement . It should be after printf("%d\n",remain[i]);.
Modified code :-
#include <stdio.h>
#include <stdlib.h>
int divise(int n)
{
int i = 0;
int remain[20];
int rest;
while (n != 0)
{
rest = n % 2;
remain[i] = rest;
n = n / 2;
printf("%d\n", remain[i]);
i++; // repositioned
}
}
int main()
{
divise(10);
return 0;
}
Output :-
0
1
0
1
Your function int divise(int n) do not return any int values . So better make it void divise(int n) .Also int main() should have a return 0 .
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 5 years ago.
Improve this question
I'm receiving Output: 1. I should count the number of times a digit appear in an integer, for example, for number 1222345 and n = 2 Should appear 3 times.
int countOccurrences(int n, int num)
{
int i,k;
i=0;
while(num!=0)
{
k=num%10;
num=num/10;
if(k==n)
{
i++;
}
}
}
// Main
void main()
{
int num= 1222345;
int n = 2;
printf("Occurance of a number: %d", countOccurrences(n,num));
}
You have undefined behavior in the code. The function is supposed to return an int and it didn't.
Solution is to add return i in the end of other function. This will give you correct result. In the countOccurrences() function
...
if(k==n)
{
i++;
}
}
return i;
}
I was skipping the discussion of error check and all that. As chux mentioned for n<=0 case you might want to add a different way of handling it but you didn't add it. Atleast consider those case and put an error message on whatever input you need.
Some corner cases are
n=0,m=0.
Negative value of n or m.
Put a return on your countOccurrences function please
int countOccurrences (int n, int num) {
int i, k;
i = 0;
while (num! = 0)
{
k = num% 10;
num = num / 10;
if (k == n)
{
i ++;
}
}
return i; }
As other have pointed out, there are important issues with your code.
Here is a recursive solution that you may find interesting:
int countOccurrences(int n, int num)
{
int count = ((num % 10) == n);
return (num < 10) ? count : count + countOccurrences(n, num / 10);
}
Few general remarks about your code:
When using printf(), you should #include <stdio.h>.
main() should return int.
Place spaces around operators and format your code consistently. This k = num % 10; is more readable than k=num%10;. (There's more to code formatting than a matter of taste; without spaces you create areas full of characters which are more difficult to parse for our visual system.)
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 years ago.
Improve this question
I have this C program code here, which determines the highest contiguous value for an array:
#include <stdio.h>
int inputs[8];
int calcSum(int i, int j);
int main(void) {
int i, j, maxSum = 0, tempSum = 0;
int length = sizeof(inputs)/sizeof(inputs[0]);
for(i=0;i<length;i++) {
scanf("%d", &inputs[i]);
}
for(i=0;i<length;i++) {
for(j=i;j<length;j++) {
tempSum = calcSum(i,j);
if(tempSum > maxSum) {
maxSum = tempSum;
}
}
}
printf("%d\n", maxSum);
return 0;
}
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
Even though this code looks correct to me, it outputs a wrong result. Sampling adding any sort of printf("") (can be empty as well) between tempSum = ... and if(tempSum >...) will make the code output the correct answer for all test cases. I even rewrote the entire code from scratch and still get the same issue.
For example, the number series: 5 2 -1 -2 -4 3 5 -6 should output 8, which it does once the printf("") is added, otherwise it outputs 38...and I have no idea why. Can you please explain, where I went wrong?
In this code:
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
You need to make sure you initialize sum to a starting value:
int calcSum(int i, int j) {
int c, sum;
sum=0;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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
#include <stdio.h>
int main()
{
int i, n, c, p;
printf("enter\n");
scanf("%d", n);
c = find(n);
if (c == 1)
{
printf("no. is not prime");
}
else
{
printf("no. is prime");
}
}
find(int n)
{
int i = 2, p;
while (i < n)
{
p = n % i;
printf("value of p%d", p);
if (p == 0)
{
return 1;
}
i = i + 1;
}
return 2;
}
....................................
Above program giving me 'not a prime number' output for all inputs...also the value of p is always zero and this shouldn't be the case...
Please help...badly stuck...
Your scanf() call must take the address of n. Furthermore you primality test fails for numbers smaller than 2. It is also better to return non-zero for true, zero otherwise, so that the value can directly be tested with if. And you should find a better name than find.
Try something like this:
#define TRUE 1
#define FALSE 0
int is_prime (int n)
{
int i;
if (n < 2)
return FALSE;
for (i = 2; i < n; i++) {
if (n % i == 0) {
return FALSE;
}
}
return TRUE;
}
int main()
{
int n;
printf ("enter number: ");
scanf ("%d", &n);
if (is_prime (n)) {
printf ("number is prime.\n");
}
else {
printf("number is not prime.\n");
}
return 0;
}
Various improvements are possible but I wanted to stay as close to your code as possible.
This looks like a student exercise so let me start by suggesting that the debugger is your friend. :)
Having said that, you may want to review the Sieve of Eratosthenes and leverage Wikipedia for a source of some good test content.
As already suggested, there are loads of potential improvements... I'd modify your "find" function to be more clear as follows:
bool IsPrime(unsigned int n)
{
unsigned int nCounter = 2;
while (n % nCounter++);
return (nCounter > n);
}
Prime's can't be negative and since you're asking a "TRUE/FALSE" question, the name and return type should enforce that contract.
Several issues:
scanf("%d", n); should be scanf("%d", &n); - you need to pass the address of n so scanf can update it (note that you risk a runtime error,
since the value of n most likely isn't a valid address value);
Implicit typing of functions such as find(int n) {...} is no longer supported as of the C99 standard, and it was never good practice to begin with. You should (and for C99 and later, must) provide a type specifier along with the function name in both function declarations and function definitions - int find( int n ) {...};
Similar to 2, a function declaration must be visible before a function is called; the simplest way to accomplish this is to move the function definition above the definition for main. If you don't want to do that, then you need to add the declaration int find(int n); somewhere before find is called.
Note that you can speed up the primality test in a couple of ways. First, you can skip testing against even factors; if a number is divisible by a multiple of 2, then it's divisible by 2, and you would have already checked for that. Secondly, you don't need to test all factors up to n - 1; you only need to test factors up to the square root of n. You can put that all together like so:
if ( n < 2 )
return 0; // zero indicates false
if ( n == 2 )
return 1; // non-zero indicates true
int result = n % 2;
for ( int i = 3; result && i * i <= n; i += 2 ) // loops as long as result
result = n % i; // is non-zero, only tests
// against odd numbers up to
return result; // sqrt(n)