Hi I need help since the problem given to me is to make a code that would
Generate a 10x10 matrix using rand function to fill the said matrix with random numbers Then print all the numbers in matrix form Then ask the user the following options OPTIONS: a: Total sum per row b: Total sum per column c: Display all ODD numbers horizontally d: Display all EVEN numbers horizontally e: Display all PRIME numbers horizontally f: Generate new set of random numbers g: Close the program
All the options is working except option a,b, and f, it does not correctly compute sum of col and row and my codes do not generate new set of random numbers. Can anyone help me? Thanks!
Code:
#include<stdio.h>
#include<stdlib.h>
int isPrime(int n)
{
int c=0;
for(int i=1;i<n;i++)
{
if(n%i==0)
c+=1;
}
if(c==1)
return 1;
else
return 0;
}
int main()
{
int my_array[10][10];
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
my_array[i][j]=rand()%20;
}
}
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
printf("%d\t ",my_array[i][j]);
printf("\n");
}
while(1)
{
char ch;
printf("\nSHOW \na.) Total sum per column\nb.) Total sum per
row\nc.) Display all ODD numbers horizontally\nd.) Display all EVEN numbers
horizontally\ne.) Display all PRIME numbers horizontally\nf.) Generate a new
set of random numbers\ng.) Exit the program");
printf("\n\nOption:");
scanf(" %c",&ch);
if(ch=='a')
{
for(int i=0;i<10;i++)
{
printf("%d",i+1);
int sum=0;
for(int j=0;j<10;j++)
{
sum+=my_array[j][i];
}
printf("%d\n",sum);
}
}
else if(ch=='b')
{
for(int i=0;i<10;i++)
{
printf("%d",i+1);
int sum=0;
for(int j=0;j<10;j++)
{
sum+=my_array[i][j];
}
printf("%d\n",sum);
}
}
else if(ch=='c')
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(my_array[i][j]%2==1)
printf("%d ",my_array[i][j]);
}
}
printf("\n");
}
else if(ch=='d')
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(my_array[i][j]%2==0)
printf("%d ",my_array[i][j]);
}
}
printf("\n");
}
else if(ch=='e')
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(isPrime(my_array[i][j])==1)
printf("%d ",my_array[i][j]);
}
}
printf("\n");
}
else if(ch=='f')
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
my_array[i][j]=rand()%20;
}
}
}
else if(ch=='g')
{
break;
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire