I have tried to remove the duplicate elements from an array
using c language, here I'am using 3 for loop to remove the duplicate elements.
How can this optimize the code?
any other simple method to remove the duplicate elements?
Thanks.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, k;
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i] == arr[j])
{
for(k=j; k<size; k++)
{
arr[k] = arr[k + 1];
}
size--;
j--;
}
}
}
printf("\nArray elements after deleting duplicates : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}
Related
Firstly, a number N has to be input, and the matrix is of NxN dimensions then. The diagonal of the matrix has to be all 0, the elements of the matrix above the diagonal have to be from 1 to N diagonally, and the elements under the diagonal need to be filled from -1 to -N also diagonally. It should be something like this (if N=5):
But the problem that I have is that I print it out like this:
and I don't know how to fix it.
This is the code that I have:
`#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d",&N);
int k=0;
for(i=0; i<N; i++){
for(j=0; j<N; j++){
if(i==j){
matrix[i][j]=0;
}
else if(j>i && i!=j){
for(k=0; k<N; k++){
matrix[k-1][j]=k;
}
}
else if(j<i && i!=j){
for(k=0; k<N; k++){
matrix[i][k-1]=-k;
}
}
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}`
I would really appreciate the help.
Here is you code modified, notice that 3 inner loops are removed with only one line.
second, you ask for the number N, however due to statically initialisation to maximum 50, you should as well verify that it is not. otherwise segmentation fault will happen.
or if you want to allow N >50 then better to do dynamic allocation on matrix.
#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d", &N);
if (N > 50){
printf("N should be smaller than 50, \n");
N = 50;
}
for(i=0; i<N; i++){
for(j=0; j<N; j++){
matrix[i][j]= j - i;
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}
SO i was solving this problem and i am getting this error
What is my mistake here?Why is it saying invalid argument type?Is there any declaration mistake I made?
I am a newbie still I am trying hard to learn these. A detailed explanation will be useful
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int m,n;
printf("Input the number of Rows: ");
scanf("%i", &m);
printf("Input the number of Columns: ");
scanf("%i", &n);
int *arr=(int*)malloc(m * sizeof(int));
for(int i=0;i<m;i++)
{
arr[i] = (int*)malloc(n*sizeof(int));
}
printf("Populate Matrix Row by Row\n--------------------------\n");
for(int i=0; i<m; i++){
printf("Row [%i]\n--------\n",i);
for(int j=0; j<n; j++){
printf("At Col [%i]= ",j);
scanf("%i", &*(*(arr+i) + j));
printf("\n");
}
}
printf("[MATRIX]\n------------------\n");
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
printf("%i ",*(*(arr+i) + j));
}
printf("\n");
}
printf("------------------\n");
printf("The duplicate value(s) are:\n");
int temp_index=0;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
temp_index=j;
for(int x=i; x<m; x++){
for(int y=temp_index; y<n; y++){
if(j!=y){
if(*(*(arr+i) + j) == *(*(arr+x) + y)){
printf("%i in position (%i, %i)\n",*(*(arr+i) + j),x,y);
}
}
}
temp_index=0;
}
}
}
free(arr);
return 0;
}
int *arr=(int*)malloc(m * sizeof(int));
*(*(arr+i) + j): *(arr+i) is int, you are trying to dereference the int value *(arr+i) + j . I assume you would wish
int **arr = malloc(m * sizeof(int*));
It would be more clear and shorter if you had used arr[i][j] instead of *(*(arr+i) + j).
I am trying to output my 2D array into an Ascending order.
I have already output it into the order which the user put in. But can not get it to print to Ascending Order.
Could anybody possibly help with this or know a solution? My code is below for any question regarding code.
int main() {
/* 2D array declaration and size of each Array in the Programme*/
int Array[2][3];
printf ("***** Bubble Sort Assessment 2 ***** \n");
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter numeric values for each Array [%d][%d]: \n", i, j);
scanf("%d", &Array[i][j]);
}
}
/*Displaying array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , Array[i][j]);
if(j==2)
{
printf("\n");
}
}
}
printf("\n\nAscending : ");
for (int i = 0; i < 2; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (a[j] < a[i])
{
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
return 0;
}
First Use bubble sort algorithm
for(int i=0; i<size; ++i)
{
for(int j=i+1; j<size; ++j)
{
if(a[i]>a[j]))
{
int temp=a[i];
a[i]=array[j];
a[j]=temp;
}
}
}
Then print your array in an ascending order.
Try to apply your own way, you can use some features of arrays like arrays contiguity.
int *ptr=a;
same with
*(ptr+i), *(ptr+j)
To use a simple one dimensional bubble sort algorithm to sort 2D array you have to add another loop taking care of the rows.
In your case:
for(k =0; k< 2; k++) {
for (i = 0; i < 3; i++) {
for (j = i+1; j < 3; ++j) {
if (a[k][i] > a[k][j]) {
int swap = a[k][i];
a[k][i] = a[k][j];
a[k][j] = swap;
}
}
}
}
The program:
#include <stdio.h>
int main(void) {
/* 2D array declaration and size of each Array in the Programme*/
int a[2][3];
printf ("***** Bubble Sort Assessment 2 ***** \n");
/*Counter variables for the loop*/
int i, j, k;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("Enter numeric values for each Array [%d][%d]: \n", i, j);
scanf("%d", &a[i][j]);
}
}
/*Displaying array elements*/
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , a[i][j]);
if(j==2)
{
printf("\n");
}
}
}
// SORT:
for( k = 0; k< 2; k++) {
for ( i = 0; i < 3; i++) {
for ( j = i+1; j < 3; ++j) {
if (a[k][i] > a[k][j]) {
int swap = a[k][i];
a[k][i] = a[k][j];
a[k][j] = swap;
}
}
}
}
printf("\n\nAscending : ");
printf("\n The 2-D Array contains : \n");
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("%d " , a[i][j]);
if(j==2)
{
printf("\n");
}
}
}
return 0;
}
Output:
***** Bubble Sort Assessment 2 *****
Enter numeric values for each Array [0][0]:
1
Enter numeric values for each Array [0][1]:
0
Enter numeric values for each Array [0][2]:
2
Enter numeric values for each Array [1][0]:
5
Enter numeric values for each Array [1][1]:
4
Enter numeric values for each Array [1][2]:
3
The 2-D Array contains :
1 0 2
5 4 3
Ascending :
The 2-D Array contains :
0 1 2
3 4 5
At the moment I am creating a program where the user is asked to place the queens for the 8 Queens Problem. Right now I have nearly created the program, but I'm stuck on how to make the program check diagonally.
This is the (unfinished)code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int check_r_c(int**chess,int*p,int N,int M)
{
int times=0;
for(int i=0; i<N; i++)
{
times=0;
for(int j=0; j<M; j++)
{
if(chess[i][j] == 1)
times++;
}
if( times != 1)
{
*p=1;
return 1;
}
}
for(int j=0; j<M; j++)
{
times=0;
for(int i=0; i<N; i++)
{
if(chess[i][j] == 1)
times++;
}
if( times != 1)
{
*p=1;
return 1;
}
}
*p=0;
return 0;
}
int main()
{
int N,M;
printf("Give the number of rows: \n");
scanf("%d",&N);
printf("Give the number of columns: \n");
scanf("%d",&M);
int**chess = malloc(N*sizeof(int));
int i,j,ch;
int*ptr;
ptr=&ch;
if(chess==NULL)
{
printf("\nMemory cannot be allocated\n");
return 1;
}
for(i=0; i<N; i++)
{
if((chess[i] = malloc(M*sizeof(int)))==NULL)
{
printf("\nMemory cannot be allocated\n");
return 1;
}
}
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
chess[i][j]= 0;
for(int k=0; k<N; k++)
{
printf("Give the position of the %d queen\n",k+1);
scanf("%d",&i);
scanf("%d",&j);
if(chess[i][j] == 1)
{
printf("You cant put 2 queens in the same place!!!\n" );
return 0;
}
chess[i][j] = 1;
}
check_r_c(chess,ptr,N,M);
if(ch == 0)
printf("Solution is correct!");
else
printf("Solution is incorrect!");
for(int i=0; i<N; i++)
free(chess[i]);
free(chess);
}
The logic for checking diagonal for a queen placed is (p,q) is to check for all the places located at (p+i,q+i) for all i, where both p+i and q+i are within the board. For negative side, use (p-i,q-i).
Basically i want to create a 2 dimensional array size NxN, find the maximum value in each row and replace it in the upper triangle or rather replace the elements with the maximum it for that specific row, but above the diagonal.
#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM 100
int main()
{
int n, i, j, temp,m;
float a[MAXIMUM][MAXIMUM], max;
printf("dimensions: ");
scanf("%d",&n);
printf("input elements\n");
for (i=0; i<n; i++){
for (j=0; j<n; j++){
scanf("%f",&a[i][j]);
}
}
max=a[0][0];
for (i=0; i<n; i++){
for (j=0; j<n; j++)
{
if(max<a[i][j]){
max=a[i][j];
}
for(m=0;m<n;m++) //the problem starts here
{
if(max>a[i][m]){
if(i+m>n-1){
a[i][m]=max;
}
}
}
}
}
for(i = 0; i < n; i++) {
printf("\n");
for(j = 0; j < n; j++) {
printf("%f ", a[i][j]);
}
}
return 0;
}
J loop finishes every times, then m loop starts so the value of j is n-1 when it is in m loop.That is a problem.
maybe like this
for (i=0; i<n; i++){
max=a[i][0];
for (j=1; j<n; j++){
if(max<a[i][j]){
max=a[i][j];
}
}
for(m=i;m<n;m++){
a[i][m]=max;
}
}