Any one can help me to this code

I have no idea to get the index location of the variables! any one can help me

this is my code that i made but getting the index is my problem!

do you think this is the correct code



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
main(){
       int TwoDi [2][3];
       int row=0;
       int col=0;
       int numb=0;
       int loc=0;
       int val=0;
       
       for(row=0;row<2;row++){
          for(col=0;col<3;col++){
              printf("The Variable is ");
              scanf("%d",&val);
          }
              }
       for(row=0;row<2;row++){
          for(col=0;col<3;col++){                                         
             loc=TwoDi[row][col];
          }
             }
       printf("%d",loc);      
             
       
       getche();
       }


maybe this is the sample output

Enter a Number [0][0] : 87
Enter a Number [0][1] : 88
Enter a Number [0][2] : 89
Enter a Number [1][0] : 90
Enter a Number [1][1] : 91
Enter a Number [1][2] : 92

Search a Number : 90
Index Location : Row 1 Column 0


pls help me!
You made a lot of mistake in your program..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 for(row=0;row<2;row++)
    {
      for(col=0;col<3;col++)
      {
          printf("The Variable is ");
          scanf("%d",&TwoDi[row][col]); 
      }
    }
    printf("\nSearch Number");
    scanf("%d", &numb);
    for(row=0;row<2;row++)
    {
      for(col=0;col<3;col++)
      {
         if(numb == TwoDi[row][col])
            printf("Row %d and Col %d", row,col);        
      }
    }
    getchar();
    return 0;
}
Last edited on
It looks like you want to change scanf("%d", &val); to scanf("%d", &TwoDi[row][col]);

Then when you search for the location of the number you can make a comparison like:
if (num_to_search_for == TwoDi[row][col]) within your for loops.

I hope this helps you get on track. If it would be more useful, then I can post a working solution.

Edit:
I guess HiteshVaghani1 started his post before I did, or something. Well, there you go.
Last edited on
With addition to Hitesh's code (I hope hitesh Don't mind for some corrections also)
As it is C++ forum rhenz000, you can use following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
int TwoDi [2][3];
int numb;

for(int row=0;row<2;row++)
    {
      for(int col=0;col<3;col++)
      {
          std::cout << "Enter number a number [%d][%d]" << row, col;
          std::cin >> TwoDi[row][col]; 
      }
    }
    std::cout << "\nSearch Number";
    std::cin >> numb;
    for(int row=0;row<2;row++)
    {
      for(int col=0;col<3;col++)
      {
         if(numb == TwoDi[row][col])
            std::cout << "\nRow %d and Col %d"<< row,col;        
      }
    }
    getchar();
    return 0;
}


But still I suggest you try (error and correct method) in your implementation so that you will learn about program as well and you know what is wrong with what point.

best luck.
Last edited on
Yeh Tnx Guys you help me a lot so maybe i will study to much for the code that u give to me

i use the code of HiteshVaghani1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
main(){
       int TwoDi [2][3];
       int row=0;
       int col=0;
       int numb=0;
       int val=0;
       
       for(row=0;row<2;row++)
    {
      for(col=0;col<3;col++)
      {
          printf("The Variable is ");
          scanf("%d",&TwoDi[row][col]); 
      }
    }
    printf("\nSearch Number ");
    scanf("%d", &numb);
    for(row=0;row<2;row++)
    {
      for(col=0;col<3;col++)
      {
         if(numb == TwoDi[row][col])
            printf("The Location is : Row %d and Col %d", row,col);        
      }
    }
       getche();
       }





maybe u can help for more code specially to my final program :)
im beginner for c++ so i have no idea for some code! hmm a little idea but not enough!
Last edited on
can i ask again what is the use of

return 0;

some of codes is not already done to teach professor :) so dats why i have no idea. . anyway tnx again!
Technically, in C or C++ main function has to return a value because it is declared as "int main"
which means "main function should return integer data type"

if main is declared like "void main", then there's no need of return 0.

Some compilers even accept and compile the code even if u dont write return 0. varies from compiler to compiler.
And also return 0 means that your program executed without errors .
can i ask again what is the use of

return 0;


To expand on the existing reply, when you run your program on your standard desktop operating system, the first thing that is run is not your function main. There are things that need to be done before your code can be started. Eventually, a function in the runtime gets around to calling your function main, just as if it were any other function. The runtime expects that the function main returns an int. There will be a place in memory where it expects to find that returned value.

Bad things can happen if the function does not return a value when it is expected to. Many runtimes on everyday operating systems can handle it. They can work around it when the programmer chooses to deliberately write bad code. There are systems where it can cause serious problems.
Last edited on
Topic archived. No new replies allowed.