Why does my C program give me weird outputs? - arrays

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

Related

Number pattern printing in c

I'm newbie to programming. So as an exercise, I'm trying to print a number pattern like below.
0
10
210
3210
43210
I tried the code below.
#include<stdio.h>
void main()
{
int i ,j,n=5;
for(i=0;i<=n;i++)
{
for(j=0;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}
}
The output am getting after running the code above is:-
10
10
10
10
10
Am just stuck. Not able to solve this question. Can anyone help me please?
There are two loops.
The first loop control the line number with range as [0,N).
The second loop control the character sequences per line. Each line print out [Line_Number + 1] digital numbers. The sequence has a pattern as [Line_Number - Column_Number].
The example code:
#include <stdio.h>
void main() {
int i, j, n = 5;
for (i = 0; i < n; i++) {
for (j = 0; j < (i + 1); j++) {
printf("%d", (i - j));
}
printf("\n");
}
}
Build and run:
gcc test.c
./a.out
The output:
0
10
210
3210
43210
You have done a simple mistake for the inner loop-j. Make sure that your outer loop i-loop refers to the number of lines and your printing as printf("%d",i); defines how many lines you want.
#include<stdio.h>
void main()
{
int i ,j,n=5;
for(i=0;i<n;i++)
{
for(j=i;j>=0;j--)
{
printf("%d",j);
}
printf("\n");
}
}
Then the output will be:
0
10
210
3210
43210
#include<stdio.h>
void main()
{
int i ,j,n=5,t[n];
for(i=0;i<n;i++)
{
t[i]=i;
for(j=i;j>=0;j--)
{
printf("%d",t[j]);
}
printf("\n");
}
try this...

My C code for a genetic algortihtm is not functioning, it dosent enter into the if condition

This is GA for a timetable problem. I'm trying to create an initial population, but it isn't working as it isn't entering the if condition. can someone point out the error?
I tried inserting statements in each condition, but everything checks out. Still, I don't seem to find a solution.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
int random_number_creator(int upper, int lower)
{
int n;
n = rand() % (upper-lower)+ lower;
return n;
}
struct pop{
int subjects[6];
int days[5][9];
}oldpop, bench;
main()
{
int i,j,s=1,d,h,stop=1,cou=0;
for(i=0;i<5;i++)
{
for(j=0;j<9;j++)
if(j!=6)
bench.days[i][j]=0;
else
bench.days[i][j]=11111;
}
for(i=0;i<6;i++)
{
if(i<4)
oldpop.subjects[i]=3;
else
oldpop.subjects[i]=2;
}
for(i=0;i<5;i++)
{
printf("\n");
for(j=0;j<9;j++)
printf(" %d",bench.days[i][j]);
}
for(i=0;i<6;i++)
{
printf(" \n %d",oldpop.subjects[i]);
}
cou=0;
for(i=0;i<6;i++)
{
cou=cou+ oldpop.subjects[i];
}
printf("\n%d\n",cou);
do
{
s=random_number_creator(5,0);
printf("\nsubject number:%d\n",s);
printf("\nloop 1 entery %d",cou);
do
{
printf("\nloop 2 entry\n");
d=random_number_creator(5,0);h=random_number_creator(8,0);
printf("\nDay Number:%d \nHour Number:%d\n",d,h);
if(bench.days[d][h]==0&&oldpop.subjects[s]!=0)
{
printf("\nif condition reached\n");
oldpop.days[d][h]=10+s;
bench.days[d][h]=11111;
stop=0;
cou--;
oldpop.subjects[s]--;
}
else
{
printf("\nIf condition not satisified.\n");
break;
}
}while(stop!=0);
}while(cou!=0);
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
}
I want the oldpop variable to be initialized for this timetable problem but the code does not enter the if condition in the do while loop.
The problem in your program comes from the subject selection (after adding the missing }):
s=random_number_creator(5,0);
Will return a random number between 0 and 4 included.
To correct this, just replace this line by
s=random_number_creator(7,0);
To pick a number between 0 and 6. So the cou variable will be able to reach 0
Your code can be improved:
Instead of this kind of block:
for(i=0;i<5;i++)
{
printf("final entery \n");
for(j=0;j<9;j++)
printf(" %d",oldpop.days[i][j]);
}
Create a function, learn to use printf:
void print_table(struct pop pop, const char *label)
{
int i, j;
printf("\n%s\n", label);
for (i=0;i<5;i++)
{
for (j=0;j<9;j++)
{
printf(" %5d",pop.days[i][j]);
}
}
}
And use it this way
print_table(oldpop, "oldpop");

Program to remove the duplicate elements from an array in c

I've tried to write a program that removes the duplicate values from an array. I've partly managed to do so since my program is able to remove any ONE of the numbers which are repeated TWICE in the array. So the problem is that if a number is repeated thrice only one of the number is removed, i.e. the other two is still left in the array, also if more than one number is repeated even then only the number which comes first in the array is removed. I really cannot understand what's wrong with my code and why is it unable to remove numbers that are repeated more than two times. I've already surfed through the internet regarding this issue and though I got different ways to remove the duplicate elements, I still don't know what's wrong with my code.
#include <stdio.h>
#include <stdlib.h>
int dup(int [],int);
int main()
{
int i,n,index,a[20];
printf("Enter n value \n");
scanf("%d",&n);
printf("Enter array values \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(i=index;i<n;i++)
a[i]=a[i+1];
n-=1;
}
}
printf("Output: \n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);
return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
int i,j,pos=-1;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
pos=j;
return pos;
}
}
}
if(pos==-1)
return pos;
}
OUTPUT
Enter n value
5
Enter array values
12
24
3
12
24
Output:
12
24
3
24
It clearly fails to remove the other repeated element "24". Also if a number was repeated thrice only one of the number would be removed.
for(i=0;i<n;++i) // <-------------------------------------- for i
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(i=index;i<n;i++) // <--------------------------- for i
a[i]=a[i+1];
n-=1;
}
}
You are using the same loop variable for two loops, one nested inside the other. This cannot work. Use different variables. Live demo.
The Problem seem to lie in the if condition in second loop.
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
Simply put this piece of code after your if condition
if(a[i]==a[j])
and it will work.
My mistake, at first glence I thought you had problem with n after running this it worked.
#include <stdio.h>
#include <stdlib.h>
int dup(int [],int);
int main()
{
int i,n,index,a[20], count;
printf("Enter n value \n");
scanf("%d",&n);
count = n;
int j;
printf("Enter array values \n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
{
index=dup(a,n);
if(index==-1)
{
printf("No duplicate elements");
break;
}
else
{
a[index]=0;
for(j=index;j<n;j++)
a[j]=a[j+1];
n-=1;
}
}
printf("Output: \n");
for(i=0;i<n;++i)
printf("%d\n",a[i]);
return (EXIT_SUCCESS);
}
int dup(int a[],int size)
{
int i,j,pos=-1;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
pos=j;
return pos;
}
}
}
if(pos==-1)
return pos;
}
OUTPUT
Enter n value
5
Enter array values
12
24
3
12
24
Output:
12
24
3
You should name your iterator variables better so you might not confuse them in nested loops, or as you do, use the same twice in a nested loop.
This skips all variables after your first removal.
and you don't have to do this
if(pos==-1)
return pos;
skip the if as it is not necessary and if at this position posis not -1then you would have no return which would be UB I think.

How to find the maximum product of two prime numbers in an array?

#include <stdio.h>
int final[3];
int checkprime(int n,int loopcount)
{
int flag=0,m;
for(m=2; m<=loopcount; m++)
{
if(n%m==0 && n!=2)
{
flag=1;
return 0;
break;
}
else if(n==2)
{
flag=0;
break;
}
}
if(flag==0)
{
return 1;
}
}
int main()
{
int test_no,n,i,j,k,m,max=1,loopcount,product=1,max_available=0,count=0;
scanf(" %d",&test_no);
for(i=0; i<3; i++)
{
scanf(" %d",&n);
int array[n];
for(j=0; j<n; j++)
{
scanf(" %d",&array[n]);
loopcount=array[n]/2;
if(max<loopcount)
{
max=loopcount;
}
}
loopcount=max;
max=array[0];
for(j=0; j<n; j++)
{
int x=checkprime(array[j],loopcount);
if(x==1)
{
if(array[j]>=max)
{
max=array[j];
}
}
}
product=product*max;
max=1;
for(j=0; j<n; j++)
{
int x=checkprime(array[j],loopcount);
if(x==1)
{
if(array[j]>max && product!=array[j])
{
max=array[j];
max_available=1;
}
else if(array[j]>=max && product==array[j] && max_available==0)
{
max=product;
}
}
if(x==0)
{
count++;
}
}
if(count==n || count==n-1)
{
final[i]=-1;
}
else
{
product=product*max;
final[i]=product;
}
product=1;
}
for(i=0; i<3; i++)
{
printf("%d\n",final[i]);
}
return 0;
}
The above code is not working properly ,I am unable to get the reason behind this , I am finding prime number two times and I have used a variable loopcount so as to find the number of iterations for checking whether the number is prime or not .
I have initially taken an array of size 3 and I am providing 3 inputs with each input of different array size and then I iterate twice for finding the maximum product , If I don't find any prime number , I output -1 on the output screen .
Th first line depicts the total number of inputs which can be viewed as the number of test cases and for each test case , the first line depicts the size of array and the second line depicts the elements present in the array .Please help me to identify the problem , for the first input it is printing -1 but with some issues , Ideally , it should only go to if part
if(count==n || count==n-1)
{
final[i]=-1;
}
but it is going to code snippet of else part ,
else
{
product=product*max;
final[i]=product;
}
This I checked by writing printf statements in the else part , so I am able to get the value printed for the first input ,this is quite confusing since when if part is getting executed then why is the else part getting executed ?And for the rest inputs , it is printing garbage values .
Here is a input set:
3
5
1 4 6 8 10
3
2 2 9
2
156 13
The corresponding output is:
-1
4
169
You should scan array[j] instead of array[n]. nth index is not valid.
int array[n];
for(j=0; j<n; j++)
{
scanf(" %d",&array[n]); /// Problem is here
loopcount=array[n]/2;
if(max<loopcount)
{
max=loopcount;
}
}
Previously, you had a far complex idea. Here is some optimizations done to your code. Feel free to ask anything. Hope it helps :)
#include <stdio.h>
#include <math.h>
int checkprime(int n)
{
int flag=0,m;
for(m=2; m<=sqrt(n); m++)
{
if(n%m==0)return 0;
}
return 1;
}
int main()
{
int test_no,n,i,j,k,m,max=1,loopcount,product=1,max_available=0,count=0;
scanf("%d",&test_no);
for(i=0; i<test_no; i++)
{
scanf(" %d",&n);
int array[n];
for(j=0; j<n; j++)
{
scanf("%d",&array[j]);
}
sort(array); // Use any sort algorithm you wish to sort the array
int _1=1, _2=1;
for(int j=n-1; j>=0; j--){
if(checkprime(array[j])==1){
if(_1==1){
_1=array[j];
}
else if(_1!=1 && _2==1){
_2=array[j];
break;
}
}
}
if(_1==1 && _2==1)printf("-1\n");
else if(_2==1 && _1!=1)printf("%d\n", _1*_1);
else printf("%d\n", _1*_2);
}
return 0;
}

Kattis - Odd Man Out - Segmentation Fault

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];?

Resources