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 am trying to solve leetcode 506. (Relative Ranks problem).
This is the c code not complete yet,and there's a puzzle that output is unexpected.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,j,nums[]={5,4,3,2,1},tmp;
char str[21];
char **ret=(char **)malloc(sizeof(*ret)*5);
for(i=0;i<5;i++) //bubble sort
{
for(j=5-1;j>i;j--)
{
if(nums[i]>nums[j])
{
tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
}
}
for(i=0;i<5;i++)
{
if(i<3)
{
if(i==0)
*(ret+i)="Gold Medal";
else if(i==1)
*(ret+i)="Silver Medal";
else
*(ret+i)="Bronze Medal";
}
else
{
sprintf(str,"%d",nums[i]);
*(ret+i)=str;
//printf("%s ",*(ret+i));
}
}
for(i=0;i<5;i++)
printf("%s ",*(ret+i));
}
I think the output should be:
Gold Medal
Silver Medal
Bronze Medal
4
5
but the actual output is:
Gold Medal
Silver Medal
Bronze Medal
5
5
ans:
else
{
char *str=malloc(sizeof(char)*10);
sprintf(str,"%d",nums[i]);
*(ret+i)=str;
//printf("%s ",*(ret+i));
}
You are assigning str twice in your first loop. ret[3] and ret[4] both point to the same memory address. When you output your array, in your second loop, you will therefore get 5 twice, because its value is overwritten in the previous loop.
The statement *(ret+i) = str; does not copy the value of the variable str, but simply points ret+1 to the address of str. The address of str does not change when using sprintf to assign values to its bytes.
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
I am new to C can anyone help me how to fix this code.
I am getting a error:
'else' without a previous if
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20];
char str2[20];
int value;
printf("Enter the string: ");
scanf("%s",str1);
printf("Enter the string: ");
scanf("%s",str2);
value=strcmp(str1,str2);
if (value==0);
printf("The strings are same");
else
{
printf("The strings are not same");
}
return 0;
}
The problem here is that the semicolon after the if statement
if (value==0);
evaluates to: if value is equal to 0, do nothing.
The following line
printf("The strings are same");
not only would get executed every time, but also breaks the if-else apart so that the following else statement does not find any related if statement.
You could use braces here instead to prevent such problems in the future.
if (value == 0) {
printf("The strings are same");
} else {
printf("The strings are not same");
}
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
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i,limit,sum=0;
int a[100];
setbuf(stdout,NULL);
printf("enter the limit");
scanf("%d",&limit);
printf("enter the values");
for(i=0;i<limit;i++)
{
scanf("%d",&a[limit]);
}
for(i=0;i<limit;i++)
{
sum=sum+a[i];
}
printf("the sum is : %d",sum);
return 0;
}
Output:
enter the limit3
enter the values3
3
3
the sum is : 19265880
The actual output should be 9
for(i=0;i<limit;i++)
{
scanf("%d",&a[limit]); //replace limit with i
}
You can see that scanf is reading into the array a[limit] instead of a[i]. This is causing the issue.
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
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr;
int n,i=3;
printf("Enter number of elements you want in array:");
scanf("%d",&n);
ptr=(int *)malloc(n*sizeof(n));
if(ptr=NULL)
{
printf("Memory Not Allocated!!");
return 0;
}
else
{
printf("Memory allocation succesful\n");
int *arr;
printf("write elements here\n");
for(arr=ptr;arr<ptr+n;arr++)
{
scanf("%d",arr);
printf("Hello world");
printf("\n");
}
for(arr=ptr;arr<ptr+n;arr++)
{
printf("%d",*arr);
}
free(ptr);
return 0;
}
}
Whenever I run this code so while taking input it stops working. So I tried in later checked in an online compiler, it gives Segmentation Fault.
Please help me out!
This line:
if(ptr=NULL)
Should be:
if(ptr==NULL)
Otherwise, ptr gets assigned the value of NULL and ptr=NULL evaluates to false. Hence, the else clause is run immediately after the ptr is assigned to NULL.
One other thing, to improve the readability of your for-loop, this is a bit more readable:
for(int i = 0; i < n; i++)
{
scanf("%d",&ptr[i]);
The for statement is standard for saying, loop n times. The use of &ptr[i] (or ptr+i) makes it a lot more obvious which address is being referenced.
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
(Write a C function that should check if a matrix (4x5) is sparse or not. Knowing that: sparse matrix is a matrix that has zeros more than the half of its size.)
That's a problem for a sheet in out subject
here is my code:
#include <stdio.h>
#include <stdlib.h>
int Spare(int [4][5]);
int main()
{
int arr[4][5];
int m,n;
for(m=0;m<4;m++){
for(n=0;n<5;n++){
scanf("%d ",&arr[4][5]);
}
}
Spare(arr[4][5]);
return 0;
}
int Spare(int Arr[4][5]){
int i,j;
int zerocount=0;
for(i=0;i<4;i++){
for(j=0;j<5;j++){
if(Arr[i][j]==0){
zerocount++;
}
}
}
if(zerocount>=10) return 1;
else return 0;
}
Its Running but after the user enter inputs of array it stops working!
Any Help Guys?
Change scanf("%d",&arr[4][5]) into scanf("%d",&arr[m][n]). You are just storing the input in arr[4][5] everytime in the loop.
The Spare(arr[4][5]) pass the element of fifth row and sixth column only which doesn't exist.
The Spare function expects an array as parameter and you are passing the single element only.
The function call must be done in this way: Spare(arr)
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>
#include<string.h>
int main()
{
char string1[100],string2[100];
int count,i=0,j=0;
gets(string1);
gets(string2);
for(i=0;i<strlen(string1)-1;i++)
{
for(j=strlen(string2)-1;j<0;j--)
{
if(string1[i]==string2[j])
{
printf("They are reverse of each other");
}
else
printf("They are not");
}
}
}
/* I am trying to check if ABC and CBA are equal by checking the first index of string1 and last index of string2 and incrementing and decrementing resp. */
Try this code, it is used check both stings are reverse to each other or not.
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100],s2[100];
int count,i=0,j=0, flag=1;
gets(s1);
gets(s2);
int l1=strlen(s1), l2=strlen(s2);
if(l1==l2)
{
l2--;
for(i=0;i<l1;i++)
{
if(s1[i]!=s2[l2-i])
{
flag=0;
break;
}
}
if(flag)
printf("Both are reverse to each other\n");
else
printf("Not revrese to each other\n");
}
else
printf("Not revrese to each other\n");
}