Please Note that this problem is for homework.
With the following code, I am inputting the data on the Kattis website for this problem. The code compiles, however, I'm getting a segmentation fault after the 'Number of Partygoers #:' printf, and I'm not sure why. Please help!
In Kattis, the description of the problem is basically: There are a number of party goers, you realize someone isn't supposed to be here because you sent all of the invitations to couples. Each party goer has a code on their invitation that matches another's. Find the odd man out.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N,i,n,j,a,count=0,num=1;
int guest[1000],cas[1000];
scanf("%d",&N);
printf("Initial N: %dn",N);
while(N!=0)
{
printf("While #: %dn",N);
scanf("%d",&n);
printf("Number of Partygoers: %dn",n);
for(i=0;i<n;i++)
{
scanf("%d",guest[i]);
printf("Goer's Id: %dn",i);
guest[i]=cas[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(cas[i]==guest[j])
{
printf("Is Equaln");
if(count==2)
{
break;
i++;
j=0;
printf("Count Equaln");
}
count++;
j++;
}
else
{
printf("Case #%d: %dn",num,cas[i]);
}
}
}
N--;
num++;
}
return 0;
}
scanf is expecting a pointer and you are passing a value.
Change
scanf("%d",guest[i]);
to
scanf("%d",&guest[i]);
There is another problem, you get a value with scanf but then you overwrite this value with an uninitialized value (garbage):
for(i=0;i<n;i++){
scanf("%d",&guest[i]);
printf("Goer's Id: %d\n",i);
guest[i]=cas[i]; /* cas[i] is used uninitialized */
}
Do you mean cas[i]=guest[i];?
Related
i am new to C and i was trying to write a code that removes all the duplicate from and array of integers and print the result. But no matter what input i give it returns an output that dosent makes any sense. For example if i give the input 1,1,2,2,3 it gives me the output 1,48 instead of 1,2,3. The output dosen't make any sense. Can anybody please tell me what am i doing wrong. It might be very silly mistake i am not seeing. So irrespective of the input the first duplicate gets removed but the the code prints 48 and exits.
#include <stdio.h>
int main()
{
int arr[5],i,j;
printf("Enter the values of the array\n");
for(i=0;i<5;i++)
{
scanf("%d\n", &arr[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]==arr[j])
{
arr[j] = 0;
}
}
}
for(i=0;i<5;i++)
{
if(arr[i] > 0)
{
printf("%d ", arr[i]);
}
}
return 0;
}
hey there is nothing wrong in your code.
i have verified in a online compiler and got the expected output.
make sure you give only 5 inputs.
here is what ive checked
programme:
#include <stdio.h>
int main()
{
int arr[5],i,j;
printf("Enter the values of the array\n");
for(i=0;i<5;i++)
{
scanf("%d\n", &arr[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]==arr[j])
{
arr[j] = 0;
}
}
}
for(i=0;i<5;i++)
{
if(arr[i] > 0)
{
printf("%d ", arr[i]);
}
}
return 0;
}
output:
Enter the values of the array
1 1 2 2 3 5
1 2 3
I have tried the following code
#include<stdio.h>
int main(void)
{
int t,i, N, j,count=0;
printf("t\n");
scanf("%d",&t);
for(i=0;i<t;i++)
{
printf("N\n");
scanf("%d",&N);
for(j=1;j<=N;j++)
{
if(N%j==0)
{
count++;
}
}
if(count==2)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
But it just prints the first test case correctly, for eg. I gave the first test case as 3 it Prints yes, and 7 as the second test case it prints NO. What's the issue I am not able to figure out with this code.
Similarly, if I enter the first test case as 7 it prints YES, and enters 3 as the second case it prints NO.
You forgot to initialize count for each query. Add initialization to fix.
printf("N\n");
scanf("%d",&N);
count=0; /* add initialization here */
for(j=1;j<=N;j++)
{
can anyone help me this c program.i am trying to execute this palindrome check code but its not getting executed after i enter number.is there any error?
#include<stdio.h>
int main()
{
int num,rev=0,r,temp;
printf("enter the number: ");
scanf("%d",&num);
temp=num;
while(num>0)
{
r=num%10;
rev=(rev*10)+r;
temp=temp/10;
}
if(num==rev)
{
printf("the number is palindrome %d: ",temp);
}
else
{
printf("%d is not a palindrome",temp);
}
return 0;
}
the block shows nothing, neither it stops executing.i tried it in code block and some online websites.
I believe you have an infinite loop here as num does not change within the loop, so the outcome of num>0 never changes:
while(num>0)
{
r=num%10;
rev=(rev*10)+r;
temp=temp/10;
}
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
i still don't get this issue but its working perfectly.
So I need to make a staircase but I clearly have something wrong with my logic. Any advice on how to approach this? I only end up getting squares.
#include <stdio.h>
int main()
{
int a,b,z,y,p;
char x;
scanf("%i ", &a);
printf("Number of stairs is: %i\n", a);
printf("up: \n");
for(b=0; b<a; b++) {
for(z=1; a>=z; z++) {
x='x';
p=1;
if ((p=z)) {
printf("%c", x);
}
else {
printf(" ");
}
p++;
}
printf("\n");
}
}
Your code has many flaws and lack of clarity is one of them. Nevertheless, the reason you have a square is because p=z is always true (setting the value of p to z returns the value of z and this, in your code is always 1 or higher - a true value for C standards).
Here is a code that works, loosely based on your example:
#include <stdio.h>
int main() {
int numberSteps,currentStep,currentColumn;
scanf("%i", &numberSteps);
printf("Number of stairs is: %i\n", numberSteps);
printf("up: \n");
for(currentStep=0; currentStep<numberSteps; currentStep++) {
for(currentColumn=0; currentStep>=currentColumn; currentColumn++) {
printf("x");
}
printf("\n");
}
}
Please notice that I changed the variable names so that they became meaningful and also got rid of the unnecessary variables and the unnecessary test if ((p=z)) that was cluttering your code.
I'm trying to make the letters of a matrix sort alphabetically and then be written out in a single string.For instance you type ten words,which are then stored in an array,and every letter has its place in the matrix then,right?But after I've written the words I want to bunch all the letters of all words together and then type all the letters out in alphabetical order.This is what I have so far:
#include <stdio.h>
#include <conio.h>
int main(void){
int i, j, k, f, n, m;
//was trying out various things,that's why I have so many useless ints up there
char word[10][15],temp;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n])
{
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
}
}
printf("Letters alphabetically sorted: ");
for(i=0;i<=9;i++){
for(j=0;j<=14;j++){
printf("%d",word[i][j]);
}
}
printf("\n");
getch();
}
I'm still in the process of learning about matrixes and I've gotten pretty familiar with arrays by now.But the sorting thing is confusing me,this was my attempt but it doesn't work.It lets you write all the words,and then it crashes.
What am I doing wrong here?And how do I correct it?
In your code here:
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
the variables n and f are used uninitialised. That will most likely be the cause of the crash.
f,n are uninitialized. It has garbage and is the reason for crashing at this point.
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n]) // f,n are uninitialized and are error prone
I think this will work..Please excute and tell me..
void main()
{
char word[10][15],temp,sorted_word[15];
int i,j,ii,k,l=0;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;word[i][j]!='\0';j++)
{
ii=i;
for(k=j+1;1;k++)
{
if(ii==9 && word[ii][k]=='\0')
break;
if(word[ii][k]=='\0')
{
ii++;
k=0;
}
if(word[i][j]>word[ii][k])
{
temp=word[i][j];
word[i][j]=word[ii][k];
word[ii][k]=temp;
}
}
sorted_word[l++]=word[i][j];
}
}
sorted_word[l]='\0';
printf("%s",sorted_word);
getch();
}
here the
for(i=0;i<=9;i++)
{ printf("type in wword %d: ",i+1);
gets(word[i]);
}
gets (word[1]);
stores the value from word[1] onwards but where as the character array starts from
word[0].
may be this is not the full solution for u problem
this issue may help u in solving your doubt.