C Programm output completely changed due to unrelated, empty printf statement [closed] - c

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;
}

Related

C simple question of getting average value from array each components addition [closed]

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.

store the rest of a successive division in a array [closed]

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 .

Understanding this output from a factorial recursion [closed]

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.

C Function: Count the occurrences of a digit in an integer [closed]

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.)

Finding the maximum value in a matrix (C programming) [closed]

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'm learning to code in C. I've wrote the following code to find the maximum value inside a matrix, but for some reason the program would return the highest value in the first row (87), except the desired 99. I can't find the flaw in the code. Would be really happy for some help!
#include <stdio.h>
int Maxmin(int a[][4], int row, int col) {
int i, j, max;
max = a[0][0];
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (a[i][j] > max)
max = a[i][j];
}
return max;
}
}
void main() {
int a[3][4] = {
{ 3, 87, 11, 23 },
{ 99, 78, 19, 44 },
{ 59, 60, 13, 14 }
};
int num;
num = Maxmin(a, 3, 4);
printf("%d\n", num);
}
Not really easy to spot as usually bugs like this are down to i & j typos.
The return max; is inside the row's for loop.
Move it to the end of the function and then you'll examine every row of the matrix. I actually compiled and ran this fix.
The way to discover such errors, is to either put in extra print statements, like :
printf( "a[%d][%d]=%d ", i, j, a[i][j]);
Or to step through your program with a debugger, setting a break point on the code you're interested in.

Resources