Computer Programming | Education

C Programming | Command Line & arrays| Questions

Here are some question and answers on Command Line Arguments and Multidimensional Arrays in C language.

Command line and multidimensional arrays | Questions

Recommended Books for C Programming

To checkout books click on the images below

Question 1

Write a C program to take two strings as input as command line arguments and find whether the two strings are equal.

#include<stdio.h>
#include<string.h>

int main(int argc, char *argv[]){
    
    if(argc!=3){
        printf("Enter only two strings.");
        return 0;
    }
    char *a,*b;
    a=argv[1];
    b=argv[2];

    if(strlen(a)!=strlen(b)){
        printf("The strings are not same.");
        return 0;
    }

    int n=strlen(a);

    for(int i=0; i<n; i++)
        if(a[i]!=b[i]){
            printf("The strings are not same.");
            return 0;
        }

    printf("The two strings are same.");

}

Question 2

Write a C program that takes N integers as command line arguments and sort these numbers in decending order.

(Hint: The command line arguments will take the inputs as strings. Therefore, for converting these into integer type data, use the atoi() standard function which is available in ).

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[]){

    int temp,*a;
    a = (int *)malloc((argc-1)*sizeof(int));

    for(int i=1; i<argc; i++){
        a[i-1]=atoi(argv[i]);
    }

    for(int i=argc-2; i>0 ; i--){
        for(int j=0; j<i;j++){
            if(a[j+1]>a[j]){
                temp = a[j];
                a[j] = a[j+1];
                a[j+1]=temp;
            }
        }
    }
    printf("Numbers in descending order: ");
    for(int i=0; i<argc-1;i++){
        printf(" %d",a[i]);
    }
}

Question 3

Write a C program to determine whether a given square matrix is symmetric without finding the transpose of the matrix. Take the order of the matrix, followed by the matrix from the user. Access the elements of the matrix using 2D pointers.

(The matrix should be dynamically allocated using the malloc() function.)

#include<stdio.h>
#include<stdlib.h>

int main(){
    int j,i,n,**p;
    printf("Enter the order of matrix: ");
    scanf("%d",&n);
    p = (int **)malloc(n*sizeof(int *));
    for(i=0; i<n; i++)
        p[i] = (int *)malloc(n*sizeof(int));
    
    printf("Enter the elements of matrix(%d elements): ",n*n);
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
            scanf("%d",&p[i][j]);
    
    int flag = 1;

    for(i =0 ; i<n; i++)
        for(j=i; j<n; j++)
            if(p[i][j]!=p[j][i]){
                flag=0;
                i=n;
                break;
            }

    if(flag==0)
        printf("The matrix is not a symmetric matrix.");
    else
        printf("The matrix is a symmetric matrix.");
    
}

Question 4

Write a C program to display the spiral traversal of a given matrix. Take the order of the matrix from the user. Then, use the malloc() function to dynamically allocate memory for the matrix. Finally, take the matrix from the user as input and display its spiral traversal. Access the elements of the matrix using 2D pointers. Spiral traversal of a matrix is illustrated in the following figure:

The output for the above matrix should be displayed in the following manner:

The spiral traversal of the given matrix is as follows: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10.

// Header files
#include<stdio.h>
#include<stdlib.h>

// main function
int main(){
    // variables
	int **p,i,j,n,chk_row=0,chk_col=0;

    // accepting the order of matrix
    printf("Enter the order of matrix: ");
    scanf("%d",&n);

    // dynamic allocation of marix
    p = (int **)malloc(n*sizeof(int *));
    for(i=0; i<n; i++)
        p[i] = (int *)malloc(n*sizeof(int));
    
    // accepting the elements of matrix
    printf("Enter the elements of matrix(%d elements) in one line: ",n*n);
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
            scanf("%d",*(p+i)+j);

    // input matrix
	printf("\n\nInput : \n");
	for(i=0 ; i<n ; i++){
		printf("\n");
		for(j=0 ; j<n ; j++)
			printf("%d ",*(*(p+i)+j));
	}

    // output
	printf("\n\nOutput : \n");
	int chk = n*n;
	i=0;
	while(chk>0){
		for(j=chk_col; j<n-chk_col ; j++){
			
			printf("%d ",*(*(p+i)+j));
			chk--;
			if(chk==0){
				printf("\n\n");
				return 0 ;
			}
				
		}
				
		
		for(++i,--j;i<n-chk_row;i++){
			
			printf("%d ",*(*(p+i)+j));
			chk--;
			if(chk==0){
				printf("\n\n");
				return 0 ;
			}
		}
	
		for(--j,--i;j>=chk_col ; j--){
			
			printf("%d ",*(*(p+i)+j));
			chk--;
			if(chk==0){
				printf("\n\n");
				return 0 ;
			}
		}
	
		for(++j,--i;i>chk_row ; i--){
		
			printf("%d ",*(*(p+i)+j));
			chk--;
			if(chk==0){
				printf("\n\n");
				return 0 ;
			}
		}
		
		i++;
		chk_row++;
		chk_col++;
	}

	printf("\n\n");
}

Question 5

Write a C program that takes two sorted arrays (not necessarily of the same size) from the user and merge them to from a single sorted array by using a recursive function.

Example:

  • Enter the first sorted array: 5 6 9
  • Enter the second sorted array: 3 7 8
  • The merged sorted array is: 3 5 6 7 8 9.
// header files
#include<stdio.h>
#include<stdlib.h>

// merge function
void merge(int *a, int *b, int *c, int i, int j,int n, int m){
    if(i+j==n+m)
        return;
    if(i==n){
        c[i+j]=b[j];
        j++;
    }

    else if(j==m){
        c[i+j]=a[i];
        i++;
    }

    if(i!=n&&j!=m){
        if(a[i]>b[j]){
            c[i+j]=b[j];
            j++;
        }

        else if(a[i]<b[j]){
            c[i+j]=a[i];
            i++;
        }

        else if(a[i]==b[j]){
            c[i+j]=a[i];
            i++;
            c[i+j]=b[j];
            j++;
        }
    }

    merge(a,b,c,i,j,n,m);
}

// main function
int main(){
    // variable declaration
    int a[100],b[100],*c,n,m,i;

    // input of first sorted array //
    printf("Enter size of first array: ");
    scanf("%d",&n);
    
    printf("Enter elements of first sorted array(in one line) : ");
    for(i=0; i<n; i++)
        scanf("%d",&a[i]);

    // input of second sorted array //
    printf("Enter size of second array: ");
    scanf("%d",&m);
    
    printf("Enter elements of second sorted array(in one line) : ");
    for(i=0; i<m; i++)
        scanf("%d",&b[i]);

    // third array
    c = (int *)malloc((n+m)*sizeof(int));

    // passing into merge function
    merge(a,b,c,0,0,n,m);

    // Output //
    printf("The first sorted array is: ");
    for(i=0 ; i<n; i++)
        printf(" %d",a[i]);

    printf("\nThe second sorted array is: ");
    for(i=0 ; i<m; i++)
        printf(" %d",b[i]);

    printf("\nThe merged sorted array is: ");
    for(i=0 ; i<m+n; i++)
        printf(" %d",c[i]);
}

Recommended Books for C Programming

To checkout books click on the images below


2 Comments

  1. Hello just wanted to give you a quick heads up. The words in your post seem to be running off the screen in Chrome. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I figured I’d post to let you know. The design and style look great though! Hope you get the problem fixed soon. Cheers

    1. Thanks Odis for your suggestion. This issue appears due to different operating system and mostly appears in android systems. We will fix it. Many thanks!

Leave a Reply