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

Modify this code so that it can accept any square array: -----------------------

ID: 3740366 • Letter: M

Question

Modify this code so that it can accept any square array:

----------------------------------------------------------------------

so far I have:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int inputrow;
int inputcol;
int prod() {

return(inputrow*inputcol);
}

int test(){
return(inputrow * inputcol-1);
}

int main() {

printf("Please insert dimensions of array in AxB format ");

scanf("%dx%d", &inputrow, &inputcol);
prod();
test();
int i,j,a[inputrow+1][inputcol+1],b[test()],flag=0,r,c,x;
for(i=1;i<=inputrow;i++)
for(j=1;j<=inputcol;j++)
scanf("%d",&a[i][j]);
x=0;
b[x]=11;
while(x<=prod())
{
c=b[x]%10;
r=b[x]/10;
b[++x]=a[r][c];
if(b[x]==b[x-1])
{
flag=1;
break;
}
}
if(flag==0)
printf("NO TREASURE");
else
{
for(i=0;i<x;i++)
printf("%d %d ",(b[i]/10),(b[i]%10));
}
return 0;
}

but that seems to not work in every case

Explanation / Answer

following is the generic code for your problem.

please re-run for your desired input.

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

int main() {

    printf("enter squere array size ");   //as per it is squre array
    int n;
    scanf("%d",&n);
  
    int i,j,a[n][n];
    int b[n*(n-1)]; // size of b should be n*(n-1) as per referenced code for n=6, it was 30
    int flag=0,r,c,x;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
    x=0;
    b[x]=2*n-1;  
    while(x<=(n-1)*(n-1))   // x should be lesser to squere of (n-1)
    {
        c=b[x]%10;
        r=b[x]/10;
        b[++x]=a[r][c];
        if(b[x]==b[x-1])
        {
            flag=1;         //set flag if TREASURE found
            break;
        }
    }
    if(flag==0)
        printf("NO TREASURE");      //print if no TREASURE found
    else
    {
        for(i=0;i<x;i++)
           printf("%d %d ",(b[i]/10),(b[i]%10));   // print TREASURE
    }
    return 0;
}

sample input

6
10 21 4 2 6 5 2 9 4 10 21 4 2 6 5 2 9 4 10 21 4 2 6 5 2 9 4 10 21 4 2 6 5 2 9 4

sample output

enter squere array size
NO TREASURE

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