Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C or C++ to perform the following tasks: (1) Create a 20 × 20

ID: 3711722 • Letter: W

Question

Write a program in C or C++ to perform the following tasks:

(1) Create a 20 × 20 matrix, say A, and fill it with random integers in the range of [0, 500].

(2) Determine if a given integer is a prime number.

(3) Find all the prime numbers in A and store them into another 1-D array, say primes.

(4) Sort primes in ascending order using selection sort algorithm.

(5) Remove duplicates from primes and store the result in another 1-D array, say uniquePrimes.

(6) Display arrays primes and uniquePrimes.

Requirements

(1) Each of the above tasks is implemented as a separate function, controlled by a main() function.

(2) You are not allowed to use any “structured control statements” (for, while, do-while, if-else, switch) except if statement without else. You can only use if-without-else and goto statements for flow control.

(3) Display the arrays (primes and uniquePrimnes) with 10 numbers per line.

(4) In the documentation of your program, describe your opinion on structured programming with or without goto.

Explanation / Answer

/* Program Documentation:

While designing a program more importance is given to Readability & maintainability.

In my opinion too much of goto statement in program make a mess of sequence of code and it also reduces the readability of program. A very large program becomes complex with excessive use of goto and it becomes difficult for the programmer to handle it and also difficult to maintained. The program design by using goto is difficult to understand.

Structured programming without goto

- Reduces the complexity of program & is easier to read and understand.

- Logical structures ensure that the flow of control is clear.

- Programs are less likely to contain logic errors & errors are more easily found.

- Improved application program design.

- Programs are more easily maintained. */

#include <stdio.h>

#include <stdlib.h>

/* Function to fill 20x20 array with random integers */

void intFill(int A[20][20])

{ int i,j;

  

i=0;

loop1: if(i<20)

{

j=0;

loop2: if(j<20)

{

A[i][j]=rand()%501; /* Generate randorm number 0 to 500 */

  

j++;

goto loop2;

} /* end of inner loop */

  

i++;

goto loop1;

} /* end of outer loop */

} /* end of function */

/* Function find all the prime numbers in array A & store them into another 1-D array */

int primeNo(int A[20][20],int primes[])

{ int i,j,k,flag,p;

  

i=0;

k=0;

loop1: if(i<20)

{

j=0;

loop2: if(j<20)

{

flag = 0;

p=2;

loopprim: if( p <= A[i][j] / 2)

{

if (( A[i][j] % p) == 0)

{

flag = 1;

goto outofloop;

}

p++;

goto loopprim;

}

outofloop: if (flag == 0)

{primes[k]=A[i][j]; k++;}

  

  

  

j++;

goto loop2;

}

  

i++;

goto loop1;

}

return(k-1);

}

/* Function sort primes array in ascending order using selection sort */

void sortSelection(int primes[], int n)

{

int i,j,min,t;

  

i=0;

loop1: if(i<n)

{min=i;

j=i;

loop2: if(j<n)

{

if(primes[min]>primes[j])

{min=j;}

  

j++;

goto loop2;

}

t=primes[i];

primes[i]=primes[min];

primes[min]=t;

i++;

goto loop1;

}

}   

/* Function will remove duplicate from primes array & store the result in another 1-D array */

int removeDup(int primes[], int n,int uniquePrimes[])

{ int i=1,uni=primes[0],k=0;

loop1: if(i<n)

{

if(uni==primes[i])

{i++;goto loop1;}

uniquePrimes[k]=uni;

k++;

uni=primes[i];

i++;

goto loop1;

}

uniquePrimes[k]=uni;

return(k+1);

}

/* Function will display the 1-D array elements on screen */

void displayArray(int array[],int n)

{int i;

loop1: if(i<n)

{

printf("%d ",array[i]);

i++;

goto loop1;

}

}

void main()

{

int A[20][20], primes[400],uniquePrimes[400],i,j,plen,unilen;

//Calling function performing different operations

  

intFill(A);

plen=primeNo(A,primes);

  

sortSelection(primes,plen);

  

unilen=removeDup(primes,plen,uniquePrimes);

  

printf(" Prime Numbers:");

displayArray(primes,plen);

printf(" Unique Numbers:");

displayArray(uniquePrimes,unilen);

} /* End of main() */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote