#include <stdio.h>
#include <conio.h>
int arasrc(double a[][], int r, int c, double s);
int main()
{
double ara[3][3];
int r, c;
//ara input
for(r = 0; r < 3; r )
{
for(c = 0; c < 3; c )
{
printf("\n\tEnter value for array (%d, %d): ", r 1, c 1);
scanf("%lf", &ara[r][c]);
}
}
//printing the ara
printf("\n\tArray = ");
for(r = 0; r < 3; r )
{
for(c = 0; c < 3; c )
{
printf("[ %6.2lf ]", ara[r][c]);
}
printf("\n\t\t");
}
//searching in ara
double s;
int found;
printf("\n\tEnter a value to search: ");
scanf("%lf",&s);
found = arasrc(ara, 3, 3, s);
if(found)
{
printf("\n\tFound at position (%d, %d).", (r 1), (c 1));
}
else
{
printf("\n\tNot found!");
}
getch();
return 0;
}
//searching in ara
int arasrc(double a[][], int r, int c, double s)
{
for(r = 0; r < 3; r )
{
for(c = 0; c < 3; c )
{
if(s == a[r][c])
{
return 1;
}
else
{
return 0;
}
}
}
}
(Firstly, I am absolutely new to programming. It's only been a few days since I started my C programing language course at my university. So, I don't know a lot of things yet.)
Coming to the question: I have to do the coding where it asks the user to give input in a 2D array. then it prints the array and asks the user to search for a value in the array. The main goal is to create another function for the "searching" part.
But I can't pass the array to the function. Don't know what the problem is. Please help me fix the issue.
CodePudding user response:
include the array size.Use the following code.
int arasrc(double a[][3], int r, int c, double s)
{
for(r = 0; r < 3; r )
{
for(c = 0; c < 3; c )
{
if(s == a[r][c])
{
return 1;
}
else
{
return 0;
}
}
}
}
CodePudding user response:
First of all, it doesn't make any sense to pass r and c as parameters and then re-used them as local loop iterators. They are supposed to correspond to the sizes of the passed array or they shouldn't have been declared as parameters but local variables.
Also note that the else { return 0; }
doesn't make sense or the program will leave the function at first iteration of the loops.
In modern C, it is recommended to pass a 2D array to a function like this:
int arasrc (int r, int c, double a[r][c], double s)
{
for(i = 0; i < r; i )
{
for(j = 0; j < c; j )
{
if(s == a[i][j])
{
return 1;
}
}
}
return 0;
}
Usage: arasrc(3, 3, ara, s);
CodePudding user response:
#include <stdio.h>
#include <conio.h>
int arasrc(int a[3][3], int s)
{
for( int r = 0; r < 3; r )
{
for(int c = 0; c < 3; c )
{
if(s == a[r][c])
{
return 1;
}
}
}
}
int main()
{
int ara[3][3];
int r , c;
//ara input
for( r = 0; r < 3; r )
{
for( c = 0; c < 3; c )
{
printf("\n\tEnter value for array (%d, %d): ", r 1, c 1);
scanf("%d", &ara[r][c]);
}
}
//printing the ara
printf("\n\tArray = ");
for(int i = 0; i < 3; i )
{
for(int j = 0; j < 3; j )
{
printf("%d",ara[i][j]);
}
printf("\n\t\t");
}
//searching in ara
int s;
int found = 0;
printf("\n\tEnter a value to search: ");
scanf("%d",&s);
found = arasrc(ara , s);
if(found = 1)
{
printf("\n\tFound ");
}
else
{
printf("\n\tNot found!");
}
return 0;
}
solution of your question you need to learn the syntax only you logic is correct