Excluding unique values from an array in C [closed] - c

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 4 years ago.
Improve this question
This code aims to exclude non-repeating values in a number list, but does not return repeats in their original sequence. How to keep all repeats with their precedence?
My code:
#include <stdio.h>
#include <stdlib.h>
int count=0;
int main ()
{
int i,j,k,x, altarray[1000],array[1000];
printf("Please enter the number of integers in your list:\n");
scanf("%d",&x);
printf ("Please enter the list of numbers\n");
for (i=0; i<x; i++)
scanf("%d",&array[i]);
printf("\nThe corrected array is...\n");
for (i=0; i<x; i++)
{
for (j=i+1; j<x; j++)
{
if(array[i]==array[j])
{
altarray[count]=array[i];
count++;
}
}
}
for (i=0; i<count; i++)
printf("%d",altarray[i]);
}

To search for duplicated number, you have to cover all elements of your array in the j for loop except the one that have the index i. So that loop should look like this:
for (j = 0; j < x; j++)
{
if ((array[i] == array[j]) && (i != j))
{
altarray[count] = array[i];
count++;
break;
}
}
I have added a break, to avoid wasting cycles after detecting the existence of the duplicated number.

Related

Why is the switch operator only reading default and not the other instructions [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 1 year ago.
Improve this question
I am working on an exercise:
You have a matrix written in a file and given some commands you have to shift
(left or right) or (up or down) in a circular way a certain row or column and after given the instructions you print it out.
This is my code so far. The problem is that it is like the switch operator is not reading anything. It is just printing "Exiting" which is in the default block.
I know that it is far from done but I just need a bit of help with my problem.
To be clear, I only need help with why the switch statement isn't working. I want to try to solve the rest of the problem myself. I just need help with that specific part.
#include <stdio.h>
#include <stdlib.h>
#define N 30
int main()
{
int v[N][N];
int i=0,p,j=0;
int command1;
int command2;
int times;
int times2=0;
FILE*file;
file=fopen("file","r");
if(file==NULL)
{
printf("ERROR in the file....");
exit(1);
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
fscanf(file,"%d ",&v[i][j]);
}
}
printf("Original Matrix: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",v[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("Choose:\n1:Rows\n2:Columns\n3:Exiting\n");
{
scanf("%d",&command1);
}
switch (command1)
{
case '1':
{
printf("Choose:\n1:Shift left\n2:Shift right\n");
scanf("%d",&command2);
if(command2==1)
{
printf("Which row:\n");
scanf("%d",&p);
printf("\n");
printf("How many times:\n");
scanf("%d",&times);
while(times2<times && i<3)
{
v[p][i]= v[p][i+1];
v[p][i+1]=v[p][i+2];
v[p][i+2]=v[p][i];
i++;
times2++;
}
printf("\nNew Matrix:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",&v[i][j]);
}
printf("\n");
}
}
}
break;
case '2':
{
printf("Something Something");
}
default:
printf("\nExiting");
}
return 0;
}
Make that case 1: and case 2: without any single quotes. When you use '1' and '2' it's checking for chars, but command1 is an int.
Make sure to add a break; statement to the second case as well so it doesn't fall through to the default: case.

Almost complete beginner to C. Please explain how can I solve this problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So the problem is:" Create a program, that inputs the elements(integers) of a matrix a[n][m] and finds the element with the smallest value amongst the the elements with the biggest value of every column."
So the way I understand it, I have to go through every column and find the biggest number in each, so I will end up with 4 numbers. From these 4 numbers I have to print out the smallest.
int main()
{
int n,m;
int A[n][m];
int B[m];
int max;
int min;
int i,j;
printf("Enter a number for n: ");
scanf("%d",&n);
printf("Enter a number for m: ");
scanf("%d", &m);
for(i=1; i<=n;i++)
{
for(j=1; j<=m;j++)
{
printf("Enter a number [%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
for(j=1; j<=m; j++)
{
max=A[j][1];
for( i=1; i<=n; i++)
{
if(max<A[j][i])
{
max=A[j][i];
B[j]=max;
}
}
}
min=B[1];
for(i=1; i<=m; i++)
{
if(min>B[i])
{
min=B[i];
printf("%d ", B[i]);
}
}
printf("Min is: %d\n", min);
return 0;
}
My question is where would the error be? It must be a logical one.
I apologize for not asking my question as clearly as most of you in this forum, but it's my first time asking here. Please if you answer, explain it like you would to a beginner.
Thanks!
C programs are generally executed from top to bottom. Therefore you cannot do this:
int n,m;
int A[n][m];
Because n and m has not been given any values. To give them values further down doesn't mean that the line int A[n][m]; will somehow get re-executed.

Why array doesn't print correct numbers? [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 4 years ago.
Improve this question
Following code:
#include <stdio.h>
int izbaciSveProste(int n, int x[], int y[])
{
int i;
int flag=0;
for(i=2; i<n/2; i++)
{
if(n%i ==0)
{
flag =1;
break;
}
}
if(flag==1)
return 0;
else
return 1;
}
int main()
{
int i,j,n,x[100],y[100];
printf("Koliko elemenata zelite u polju?\n");
scanf("%d", &n);
printf("Enter elements in array:- ");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
int len = sizeof(x)/sizeof(x[0]);
for(i=0; i<len; i++)
{
if(izbaciSveProste(x[i]))
{
for(j=i; j<len; j++)
{
x[j] = x[j+1];
}
i--;
len--;
}
}
printf("Elementi nakon brisanja su:\n");
for(i=0; i<len; i++)
printf("%d\n",y[i]);
printf("\n");
return 0;
}
Purpose of this program should be to delete all prime numbers from array x[] with n elements, remaining elements should be rewritten in array y[] and show count of elements in array y[] in the end.I believe that function is okay and error is in main() specifically in storing y[].
Your function int izbaciSveProste(int n, int x[], int y[]) requires three arguments. Your code izbaciSveProste(x[i] passes one argument. That's not much enough. The compiler tells you that fact with the error message:
error: too few arguments to function 'izbaciSveProste'
Your function prototype has 3 parameters:
int izbaciSveProste(int n, int x[], int y[])
When you call the function, you only provide 1:
if(izbaciSveProste(x[i]))
The compiler wants to get all 3.
As your function doesn't even touch the 2 array parameters, you might simply remove them from the function definition and only take 1 integer.
Another problem:
You print y[i] in your loop but you never assign any value to that array.

Why is my code skipping a whole for loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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'm still learning the basics of C and I had this assignment of coding a program that shows the product of two 2D arrays entered by the user. The idea is to not make use of memory functions like malloc.
The code worked fine but when I added an if statement to check that the rows of the first array are equal to the columns of the next one during the run-time the whole part for entering the data of the first array and showing it is skipped and it goes directly to asking for the next array. I've tried to clean the buffer and use getch() trying to force the compiler to execute that part but it keeps being skipped.
Here's the whole code :
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
int main(void);
int main (void)
#define MAXR 36
#define MAXC 18
{
int Rows_A,Columns_A,Rows_B,Columns_B,Rows_C,Columns_C;
int Counter_Rows=0,Counter_Columns=0,Counter_Multiplier=0;
int Matrix_A[MAXR][MAXC],Matrix_B[MAXR][MAXC],Results_Matrix_C[MAXR][MAXC];
printf("\n\tTo multiply two matrices both have to be the same size of rows from the first matrix and the columns from the next one. \n\tPlease enter the size of the matrices:\n");
printf("\n\tMatrix A:\n\tNumber of rows :\t\t");
scanf(" %d",&Rows_A);
printf("\n\tNumber of columns :\t\t");
scanf(" %d",&Columns_A);
printf("\n\tMatrix B:\n\tNumber of rows :\t\t");
fflush(stdin);
scanf(" %d",&Rows_B);
printf("\n\tNumber of columns :\t\t");
scanf(" %d",&Columns_B);
if (Rows_A=!Columns_B)
{
printf("\n\tMatrix B number of columns and Matrix A number of rows are not the same therefore they cannot be multiplied.");
return 0;
}
else
{
Rows_C=Rows_A;
Columns_C=Columns_B;
printf("\n\tEnter the data for Matrix A:");
for (Counter_Rows=0;Counter_Rows < Rows_A;Counter_Rows++)
{
for(Counter_Columns=0;Counter_Columns < Columns_A; Counter_Columns++)
{
printf("\n\tEnter the value of the position [%d][%d] of the matrix A: ",Counter_Rows+1,Counter_Columns+1);
scanf(" %d",&Matrix_A[Counter_Rows][Counter_Columns]);
}
}
printf("\n\tMatrix A.\n\t");//Matrix A
for (Counter_Rows= 0; Counter_Rows < Rows_A; Counter_Rows++)
{
for (Counter_Columns= 0; Counter_Columns < Columns_A; Counter_Columns++)
{
printf("[%d] ",Matrix_A[Counter_Rows][Counter_Columns]);
}
printf("\n\t");
}
fflush(stdin);
printf("\n\tEnter the data for Matrix B:");
for (Counter_Rows=0;Counter_Rows < Rows_B;Counter_Rows++)
{
for(Counter_Columns=0;Counter_Columns < Columns_B; Counter_Columns++)
{
printf("\n\tEnter the value of the position [%d][%d] of the matrix B: ",Counter_Rows+1,Counter_Columns+1);
scanf(" %d",&Matrix_B[Counter_Rows][Counter_Columns]);
}
}
printf("\n\tMatrix B.\n\t");//Matrix B.
for (Counter_Rows= 0; Counter_Rows < Rows_B; Counter_Rows++)
{
for (Counter_Columns= 0; Counter_Columns < Columns_B; Counter_Columns++)
{
printf("[%d] ",Matrix_B[Counter_Rows][Counter_Columns]);
}
printf("\n\t");
}
//Calculating product matrix C.
for (Counter_Rows=0;Counter_Rows < Rows_C;Counter_Rows++)
{
for(Counter_Columns=0;Counter_Columns < Columns_C; Counter_Columns++)
{
//initializes the Matrix C in 0's
Results_Matrix_C[Counter_Rows][Counter_Columns]=0;
for(Counter_Multiplier=0; Counter_Multiplier < Columns_A; Counter_Multiplier++)
{
Results_Matrix_C[Counter_Rows][Counter_Columns]=Results_Matrix_C[Counter_Rows][Counter_Columns]+(Matrix_A[Counter_Rows][Counter_Multiplier]*Matrix_B[Counter_Multiplier][Counter_Columns]);
}
}
}
printf("\n\tProduct Matrix C:\n\t");//Matrix B.
for (Counter_Rows= 0; Counter_Rows < Rows_C; Counter_Rows++)
{
for (Counter_Columns= 0; Counter_Columns < Columns_C; Counter_Columns++)
{
printf("[%d] ",Results_Matrix_C[Counter_Rows][Counter_Columns]);
}
printf("\n\t");
}
}
return 0;
}
This is the part that is skipped :
printf("\n\tEnter the data for Matrix A:");
for (Counter_Rows=0;Counter_Rows < Rows_A;Counter_Rows++)
{
for(Counter_Columns=0;Counter_Columns < Columns_A; Counter_Columns++)
{
printf("\n\tEnter the value of the position [%d][%d] of the matrix A: ",Counter_Rows+1,Counter_Columns+1);
scanf(" %d",&Matrix_A[Counter_Rows][Counter_Columns]);
}
}
printf("\n\tMatrix A.\n\t");//Matrix A
for (Counter_Rows= 0; Counter_Rows < Rows_A; Counter_Rows++)
{
for (Counter_Columns= 0; Counter_Columns < Columns_A; Counter_Columns++)
{
printf("[%d] ",Matrix_A[Counter_Rows][Counter_Columns]);
}
printf("\n\t");
}
you have if (Rows_A=!Columns_B) in your example. I think you want if (Rows_A != Columns_B)

Prime numbers for a given range [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 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int i, j, a, b, c=0;
scanf("%d %d", &a, &b);
for(i=a; i<=b; i++)
{
for(j=1; j<=i; j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d\n", i);
}
}
return 0;
}
The program however does not print prime numbers for a given range. Please help.
You have to reset c to 0 after every iteration. The loops should look like this
for(i=a; i<=b; i++)
{
c = 0;
...
An advice, you don't have to go till the number everytime to check for primality, you can go till square root of that number.

Resources