Need help with 2D array coding assignment I created a 2D array that is printed with random generated numbers for an assi

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Need help with 2D array coding assignment I created a 2D array that is printed with random generated numbers for an assi

Post by answerhappygod »

Need help with 2D array coding assignment
I created a 2D array that is printed with random generated
numbers for an assignment. The code is suppose to generate a random
number and ask the user if that number is in the array, if it is
then it replaces the number with a 0 and generates a new number and
asks again and if it isn't it just generates a new number and
asks again. What I am having trouble is that I need the code to
print the array again after the user answers the question. So if
the user answers yes then the code will print the array again with
the updated value then ask again, same with when the user says no.
How do I go about doing that?
Here is the code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 4
#define COLS 4
void fill_random (int array[ROWS][COLS], int max);
void print_array (int A[ROWS][COLS]);
int replaceNumber (int A[ROWS][COLS], int num);
int main (void)
{
srand (time (NULL));
int A[ROWS][COLS];
fill_random (A, 10);
print_array (A);
char res = 'n';
while (1) {
int num = rand() % 40 + 1;
printf("\nIs %d present in the array?
", num);
printf("Enter 'y' for yes, any
character otherwise: ");
scanf("%c", &res);
getchar();
if (res == 'y') {
if(replaceNumber(A,
num)==0)

break;
}
else{
res = 'n';
}
}
print_array(A);
return 0;
}
void fill_random (int array[ROWS][COLS], int max)
{
int i, j, k;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
int check = 1, l;
while (check == 1)
{
check =
0;
l = rand ()
% max + (max * i) + 1;
for(k = 0 ;
k < j; k++)
{

if(array[k] == l)

{

check = 1;

break;

}
}
}
array[j] = l;
}
}
}
void print_array (int A[ROWS][COLS])
{
int i, j;
for (i = 0; i < ROWS; i++)
{
printf
("-------------------------\n");
for (j = 0; j < COLS; j++)
{
printf ("|");
printf ("%4d ", A[j]);
}
printf ("|");
printf ("\n");
}
printf ("-------------------------");
}
int replaceNumber(int A[ROWS][COLS], int num)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (A[j] == num)
{
A[j] =
0;
return
1;
}
}
}
return 0;
}
Here is the output

Need Help With 2d Array Coding Assignment I Created A 2d Array That Is Printed With Random Generated Numbers For An Assi 1
Need Help With 2d Array Coding Assignment I Created A 2d Array That Is Printed With Random Generated Numbers For An Assi 1 (13.77 KiB) Viewed 46 times
I 9 18 L 30 | 33 | I 1 L 15 | 21 | 34 | I 4 16L 29 361 I 81 131 22 | 32L n Is 3 present in the array? Enter 'y' for yes, any character otherwise: n Is 11 present in the array? Enter 'y' for yes, any character otherwise: n Is 36 present in the array? Enter 'y' for yes, any character otherwise: y Is 38 present in the array? Enter 'y' for yes, any character otherwise: n Is 22 present in the array? Enter 'y' for yes, any character otherwise: Y Is 26 present in the array? Enter 'y' for yes, any character otherwise:
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply