Chef and Dolls MISSP - c

Here is the link to the codechef problem - Chef and Dolls
Why does my code always print the wrong answer? The output should be the number which doesn't have a match but my code always print the first element. Coudn't get the conditions right, what should be the condition?
Problem Statement
Chef is fan of pairs and he likes all things that come in pairs. He even has a doll collection in which all dolls have paired.One day while going through his collection he found that there are odd number of dolls. Someone had stolen a doll!!!
Help chef find which type of doll is missing..
Input
The first line contains the number of test cases.
Second line of the input contains the number of elements in the array.
The next n lines are the types of each doll that is left.
Output
Find the type of doll that doesn't have a pair
Example
Input:
1
3
1
2
1
Output:
2
#include <stdio.h>
int main(){
int t, N, i, m, k, z, flag=0;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++){
scanf("%d", &arr[i]);
}
int j;
for(j=0;j<N;j++){
m=arr[j];
for(k=0;k<N;k++){
if(m==arr[k] && k!=j)
{
flag=0;
}
else
{
flag=1;
break;
}
printf("%d", m);
}
}
}
return 0;
}

You break out of your loop everytime k==j. You have to check for every array member if it is present somewhere else in the array.
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
for(j=0;j<N;j++)
{
m=arr[j];
flag=1;
for(k=0;k<N;k++)
{
if(m==arr[k] && k!=j)
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
}
}
}
return 0;
}
But be careful as this only looks if an array member has a duplicate in it. You probably want to check if there is an even number of elements in the array.
If you want to check which array members occur an odd number of times
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, num, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N], odds[N], n_odds;
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
n_odds=0;
for(j=0;j<N;j++)
{
m=arr[j];
num=0;
for(k=0;k<N;k++)
{
if(m==arr[k])
{
num++;
}
}
if(num%2) //occurs an odd number of times
{
flag=1;
for(i=0;i<n_odds;i++)
{
if(m==odds[i]) //has already been checked
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
odds[n_odds++]=m;
}
}
}
}
return 0;
}

Related

My C Program is not executing fully, it's stopping midway. Question is taking elements of array from user and calculating sum of each column in array

The Question is to take dimension of array and elements of it from the user. Then calculate the sum of each column in array, it stops midway for some reason. I am new to C, is there a problem in usig malloc not in right way?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n;
int arr[m][n];
int **arr;
int i,j;
printf("Enter Number of Rows and Columns:\t");
scanf("%d %d", &m, &n);
arr=(int**)malloc(sizeof(int*)*m);
for(i=0;i<10;i++)
{
arr[i]=(int*)malloc(sizeof(int)*n);
}
printf("Type Array Elements:\t");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("This is Where It Stops");
//ADDITION OF COLUMNS
int Sum;
int *sum=(int*)malloc(j*sizeof(int));
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
Sum=0;
Sum=Sum+arr[i][j];
}
sum[j]=Sum;
}
for(j<0;j<0;j++)
{
printf("Sum of Column %d is %d", j, sum[j]);
}
return 0;
}

Function not working on c program selection sort

Why this function is not working here? It's not sorting on output.
Suppose if I enter 1 4 2 the output is always 1 4 2 not 1 2 4.
How can properly implement this selection sort?
Thanks in advance!!
#include <stdio.h>
int selection_sort (int a[],int n, int i, int j,int temp,int min){
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
min=i;
for(j=i+1;j<n;j++){
if(a[j]<a[min]){
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main(){
int i, j, n,a[20], temp,min;
printf("How many elements:\n ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Sorted array: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
selection_sort(i,j,n,a,temp,min);
}
There are 3 main issues in your code:
You are getting user input twice. One in main function:
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Another in selection_sort function:
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Fix this by removing the one in selection_sort function.
As noted in the comments section, you are returning from main function before calling selection_sort function:
return 0;
Fix this by moving it to the end of main function.
You are printing the expected results from selection_sort function before calling it:
printf("Sorted array: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
...
selection_sort(i,j,n,a,temp,min);
Fix this by calling selection_sort before printing its results.
Here is the fixed code:
#include <stdio.h>
int selection_sort (int a[], int n, int i, int j, int temp, int min)
{
for(i=0;i<n;i++) {
min=i;
for(j=i+1;j<n;j++) {
if(a[j]<a[min]) {
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main()
{
int i, j, n,a[20], temp, min;
printf("How many elements: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
}
selection_sort(a, n, i, j, temp, min);
printf("Sorted array:");
for(i=0;i<n;i++) {
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
Here is a test:
$ gcc main.c && ./a.out
How many elements: 3
Enter array elements: 1 4 2
Sorted array: 1 2 4
int main(){
int i, j, n,a[20], temp,min;
printf("How many elements:\n ");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection_sort(a,n,i,temp,min);//Not need to use j,cause j isn't used in your function
for(i=0;i<n;i++)
printf("%d\t",a[i]);//You should call your function before print your data,and also before "return 0"
return 0;
}
AND,"int i,int temp,int min" these don't need to be the parameters.You can just create them in your function

How To Skip The 2nd Input Of n

I've been messing with functions recently and i wanted to transform the following code:
#include<stdio.h>
int main()
{
int i,n,tab[100];
printf("Insert The Number Of Elements :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Give The Element N~%d:",i+1);
scanf("%d",&tab[i]);
}
printf("The Elements Are:\n");
for(i=0;i<n;i++)
{
printf("Index[%d]=%d\n",i,tab[i]);
}
return 0;
}
... into a code where i can use functions to:
Ask for the number of elements that the array is going to hold
Show the output of the array with the elements the user entered
Here's what i got to so far but it seems like it keeps asking me for the number of elements the array will hold even though it has been already entered.
Thanks in advance to those who will help <3
#include<stdio.h>
int Number_Of(int a)
{
printf("Insert The Number Of Elements :\n");
scanf("%d",&a);
return a;
}
int Array(int tab[])
{
int i,n;
n=Number_Of(n);
for(i=0;i<n;i++)
{
printf("Index[%d]=%d\n",i,tab[i]);
}
}
int main()
{
int i,n,tab[100];
n=Number_Of(n);
for(i=0;i<n;i++)
{
printf("Give The Element N~%d:",i+1);
scanf("%d",&tab[i]);
}
printf("The Elements Are:\n");
Array(tab);
return 0;
}
You are calling "Number_Of" function two times that's why you prompted to enter again.
First time you called in main function.
Second time you called in Array function.
You can do like this.
#include<stdio.h>
int Number_Of(int a)
{
printf("Insert The Number Of Elements :\n");
scanf("%d",&a);
return a;
}
void Array(int tab[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Index[%d]=%d\n",i,tab[i]);
}
}
int main()
{
int i,n,tab[100];
n=Number_Of(n);
for(i=0;i<n;i++)
{
printf("Give The Element N~%d:",i+1);
scanf("%d",&tab[i]);
}
printf("The Elements Are:\n");
Array(tab, n);
return 0;
}

Adding the same number multiple times to an empty array in C

This is a piece of code to add the same number multiple times to an empty array but when I am printing the now non empty array, I am getting some other values:
#include<stdio.h>
#include<stdlib.h>
void sort_0(int arr[100], int i, int n){
int final_array[100], c=0;
// Count the number of '0' in the array
for(i=0;i<n;i++){
if(arr[i] == 0){
c++;
}
}
// Add the c number of '0' to the final_array
for(i=0;i<c;i++){
scanf("%d",final_array[i]);
}
for(i=0;i<c;i++){
printf("%d ", final_array[i]);
}
}
int main(){
int arr[100], i, n;
// Entering the size of the array
scanf("%d", &n);
// Entering n elements into the array
for(i=0;i<n;i++){
scanf("%d", &arr[i]);
}
sort_0(arr,i,n);
return 0;
}
In the above code, the number of times 0 appears in the array is counted. Then the count is taken as the range and 0 is adding to the empty array final_array count times.
If c = 5, the final_array = {0,0,0,0,0}
Expected Output:
arr = {0,1,4,3,0}
Output = 2
I am not getting any output
Since you don't know how much 0 you'll need to add to your array_final I figured out that a better solution could be to create that array after you have the number of 0 of the first array. Also, I see no reason why you were passsing i to the function since you can simply define it in the function itself.
void sort_0(int arr[10], int n, int* c){
int i;
for(i=0;i<n;i++){
if(arr[i] == 0){
(*c)+= 1;
}
}
}
int main (void) {
int size;
printf("Enter array size: ");
scanf("%d", &size);
int arr[size];
for (int i=0;i<size;i++) {
scanf("%d",&arr[i]);
}
int c = 0;
sort_0(arr, size, &c);
printf("C is: %d\n",c);
int* final_array;
if ((final_array=malloc(c * sizeof(int)))==NULL) // should always check malloc errors
{
perror("malloc");
return -1;
}
for (int i=0;i<c;i++) {
final_array[i]= 0;
}
printf("{");
for (int i=0;i<c-1;i++) {
printf("%d,", final_array[i]);
}
printf("%d}\n",final_array[c-1]);
return 0;
}

Merge Sort-Segmentation fault

I have written a program in C for the Merge Sort but I get a Segmentation Fault and I don't know why? Could you give me a hint what could be wrong?
That's the code:
#include <stdio.h>
#include <math.h>
void Merge(int A[],int p,int q,int r){
int n1,n2,i,j,k;
n1=q-p+1;
n2=r-q;
int L[n1],R[n2];
for (i=0; i<n1; i++) L[i]=A[p+i-1];
for (j=0; j<n2; j++) R[j]=A[q+j];
i=0;
j=0;
for (k=p; k<r; k++){
if (L[i]<=R[j]){
A[k]=L[i];
i=i+1;
}
else{
A[k]=R[j];
j=j+1;
}
}
}
void Sort(int A[],int p,int r){
int q;
if (p<r){
q=floor((p+r)/2);
Sort(A,p,q);
Sort(A,q+1,r);
Merge(A,p,q,r);
}
}
int main()
{
int n,i,p,r;
printf("Give a value for n: \n");
scanf("%d",&n);
int A[n];
for (i=0; i<n; i++){
printf ("Give %d th value of the array: \n",i+1 );
scanf("%d",&A[i]);
}
printf("Give me a value for p:\n");
scanf("%d",&p);
printf("Give me a value for r:\n");
scanf("%d",&r);
Sort(A,p,r);
printf("p=%d, r=%d \n \n", p,r);
for (i=0; i<n; i++) printf("%d", A[i]);
return 0;
}
That's what I got,giving inputs at the terminal:
Give a value for n:
8
Give 1 th value of the array:
1
Give 2 th value of the array:
6
Give 3 th value of the array:
5
Give 4 th value of the array:
3
Give 5 th value of the array:
5
Give 6 th value of the array:
4
Give 7 th value of the array:
2
Give 8 th value of the array:
7
Give me a value for p:
2
Give me a value for r:
6
Segmentation fault (core dumped)
Have I done maybe something wrong at the function Sort?
A probable culprit is these two lines:
int n1,n2,i,j,k;
int L[n1],R[n2];
In the first you declare n1 and n2, but don't initialize them. That means their value is indeterminate and that you should not use those variable until you have initialized them.
However, in the next line you use them anyway and that causes undefined behavior which means your whole program is ill-formed and can not be relied upon to work properly.
Your n1 and n2 variables in function Merge are undefined prior to you instantiating variables L[] and R[].
Try this:
int n1, n2, i, j, k;
n1 = q - p + 1;
n2 = r - q;
// Now you can instantiate your arrays
int L[n1], R[n2];
#include<stdio.h>
void mergesort(int a[],int beg,int end)
{
int mid;
if(beg<end)
mid = (beg+end)/2;
mergesort(a,beg,mid);
mergesort(a,mid+1,end);
merge(a,beg,mid,end);
}
void merge(int a[],int beg,int mid,int end)
{
int i=beg,j=mid+1,index=beg,temp[100],k;
while((i<=mid) && (j<=end))
{
if(a[i]<a[j])
{
temp[index]=a[i];
i=i+1;
}
else
{
temp[index]=a[j];
j++;
}
index++;
}
if(i>mid)
{
while(j<=end)
{
temp[index]=a[j];
j++;
index++;
}
}
else
{
while(i<=mid)
{
temp[index]=a[i];
i++;
index++;
}
}
for(k=beg;k<index;k++)
{
a[k]=temp[k];
}
}
int main()
{
int a[100],n,i,j;
printf("enter size of array");
scanf("%d",&n);
printf("enter elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
for(i=0;i<n;i++)
{
printf("5d",a[i]);
}
return 0;
}

Resources