I am getting garbage values for this prob in c [closed] - c

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 trying to execute the code below, but along with my answer I am getting some garbage values. Please help me find where I made an error.
int main()
{
int n,i,j,k=0;
int a[100];
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[++k]=i;
}
}
for(j=0;a[j]!='\0';j++)
{
printf("\t%d",a[j]);
}
}

int a[100];
C does not initialize the array elements by default. So all the elements that are not assigned in your first loop will have garbage.
What you can do is:
int a[100] = {0};
This will initialize all elements to 0

In C array indexing starts from 0. Preincrement ++k will cause to start array indexing from 1. Change it to k++. Also change
for(j=0;a[j]!='\0';j++)
to
for(j=0;j < k;j++)
to print the only values you have entered.
your code should be
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[k++]=i;
}
}
for(j=0; j < k;j++)
{
printf("\t%d",a[j]);
}

Here how you can fix this. Next time please describe what you were trying to achive

Related

Sum of numbers occurring in the multiplication table of 8. What's wrong with my code? Desirable output is 440, and I'm getting 32303304 [closed]

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 6 months ago.
Improve this question
#include<stdio.h>
int main(){
int num,i,sum=0;
printf("Enter the number:");
scanf("%d",&num);
for(i=1; i<=10; i++)
{
num = num*i;
sum += num;
}
printf("%d",sum);
return 0;
}
this is the code i have written and i don't know why this is not working?
you should not change the num which is 8 in your case.
do it like this:
#include<stdio.h>
int main(){
int num,i,sum=0;
printf("Enter the number:");
scanf("%d",&num);
for(i=1; i<=10; i++)
{
sum += num*i;
}
printf("%d",sum);
return 0;
}
You’re changing num each time through the loop, so you’re not actually adding the multiples of the input you think you are. Add some logging or run it in a debugger for good visibility into the problem.

How to dynamically allocate memory in 2D array and store values? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
I am trying to allocate memory in a 2D array dynamically, but I don't know what is wrong.
Error
let input is
2 2
1 2 3 4 5 6 7 8 9 ........
then program crashes
#include<stdio.h>
#include<stdlib.h>
int main()
{
int N,M;
int i,j;
scanf("%d %d",&N,&N);
int **A = (int **)malloc(N*sizeof(int *));
for(i=0;i<N;i++)
{
A[i] = (int *)malloc(M*sizeof(int));
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
scanf("%d",&A[i][j]);
}
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf("%d",A[i][j]);
}
}
return 0;
}
Here's your problem:
scanf("%d %d",&N,&N);
When you read in the array dimensions, you read into N twice and never read into M. As a result, the contents of M are indeterminate, and attempting to read that value invokes undefined behavior.
Fix this to input a value for M:
scanf("%d %d",&N,&M);
As i said problem is in scanning N and M variables.
Change
scanf("%d %d",&N,&N);
to
scanf("%d",&N);
scanf("%d",&M);
and you are fine.
Problem was you were reading N twice while didnt read an M which was used uninitialized.
Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.
You should free all your allocated memory
for(i=0;i<N;i++)
{
free(A[i]);
}
free(A);
and dont cast malloc()'s return value becouse
Its reduntand
Adding the cast may mask failure to include the header stdlib.h, in which the prototype for malloc is found
If the type of the pointer is changed, one must fix all code lines where malloc() was called and cast
Next time if you cannot find a bug, try to use debugger and look what exactly is happening, you can see variable values, where would you clearly see that M is uninitialized and didnt change after scanf().

Program working fine for smaller values but throwing Segmentation Fault for bigger values [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
In the following program when the value of N is less than 100 the program is executing perfectly but for bigger values of N its showing segmentation fault.I sit because of less memory or anything wrong with program??
#include<stdio.h>
int main()
{
int N,iteration,MAX_ITERATONS;
int i,j,k,n,index,boundary1,boundary2;
double h[2][100][100];
int current = 0;
int next = 1;
printf("Enter the number of points\n ");
scanf("%d", &N);
boundary1=0.4*N;
boundary2=(0.6*N);
printf("The boundary is between %d and %d .\n",boundary1,boundary2);
for(k=0;k<2;k++)
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i==0)&&(j>=boundary1&&j<boundary2))
h[k][i][j]=100;
else
h[k][i][j]=20;
}
}
}
printf("Initial Values\n");
index = N/10;
for(i=0;i<N;)
{
for(j=0;j<N;)
{
printf("%.2f\t",h[0][i][j]);
j=j+index;
}
i=i+index;
printf("\n");
}
}
When N > 100, h is accessed to an index greater than 100, inside the nested for loop
h[k][i][j]=100;
but h is defined as
double h[2][100][100];
You are going out of bounds for h
If you want N as greater than 100 you need to redefine h or malloc it.

Time limit exceeded in bubble sort [closed]

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 8 years ago.
Improve this question
Problem-To sort array in ascending order
Algorithm used-Bubble sort
Error-Time limit exceeded
Compiler-ideone online editor /Codeblocks
What could be the possible alternative for this?
int a[5];
int i,t,j;
for(i=0;i<=4;i++) //for initialising the elements
{
printf("Enter 5 numbers");
scanf("%d",&a[i]);
}
for(j=0;j<5;i++) //for sorting
{
for(i=0;i<5;i++)
{
if(a[i]>a[i+1])
{
t=a[i+1];
a[i+1]=a[i];
a[i]=t;
}
}
}
for(i=0;i<=4;i++) //for printing the sorted array
{
printf("%d\n",a[i]);
}
Your loop:
for(j=0;j<5;i++) //for sorting
should say j++, so it should be
for(j=0;j<5;j++)
Your second loop:
for(i=0;i<5;i++)
should be
for(i=0;i<4;i++)

Please check my code. won't return a right value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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.
Improve this question
#include <stdio.h>
int f_totprice(int,int,int,int);
int main()
{
int menu_1;
int menu_2;
int menu_3;
int menu_4;
int totprice;
int received_money;
while (1){
printf("what do you want to order?\n");
printf("pizza: ");
scanf("%d",&menu_1);
printf("cheese: ");
scanf("%d",&menu_2);
printf("curry: ");
scanf("%d",&menu_3);
printf("soup: ");
scanf("%d",&menu_4);
printf("======================================\n");
totprice=f_totprice(menu_1,menu_2,menu_3,menu_4);
printf("total price is %d\n",totprice); /* this part returns a wrong value */
printf("received ");
scanf("%d",&received_money);
printf("Your change is %d",received_money-totprice);
}
}
int f_totprice (int a, int b, int c, int d)
{
int total;
char price[]={500,1000,1500,2000};
total=a*price[0]+b*price[1]+c*price[2]+d*price[3];
return total;
}
so, i just wrote a code to make a simple menu and the price part doesn't return the total value of my function below.. it keeps saying "the total value is -600,-300,-190" etc instead of integers. in my eyes there is nothing wrong with it. please help? :(
char price[]={500,1000,1500,2000};
Can you figure out why this cannot work?
You should find out how to turn on warnings on your compiler.
You are using char array instead of interger array, and printing the output in integer type (%d), Hence it is printing the ASCII equivalent of the sum , Please change character array to integer array.
int price[]={500,1000,1500,2000};
Your problem lies here
int f_totprice (int a, int b, int c, int d)
{
int total;
char price[]={500,1000,1500,2000};
total=a*price[0]+b*price[1]+c*price[2]+d*price[3];
return total;
}
Why are you using a char array,
Use int array

Resources