Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, i'm doing an exercise for a course about the array. My code is not working, my aim was to create an multiple-array, composed by 5 arrays, with consequential number expect for the first object of the array (it has to be 0).

So i was expecting this answer:
{0,1,2,3,4}
{0,6,7,8,9}
{0,11,12,13,14}
{0,16,17,18,19}
{0,21,22,23,24}.

But something went wrong.Here is my code and my error.

What I have tried:

This is my try:

using System;


int [,] arr = new int[5,5];
int counter = 0;

for (int i = 0;i < 5; i++){
  for (int j = 0; j < 5; j++){
    ++counter;
    arr[i,j] = (j== 0) ? 0 : counter;
  }
}

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++);

    Console.Write($"{arr[i,j]} ");
    Console.Write("\n");
}



ERROR:
Quote:
(16,28): error CS0103: The name 'j' does not exist in the current context [/Users/nimanikravan/Desktop/csharp/IntegralTypes/IntegralTypes.csproj]

The build failed. Fix the build errors and run again.
Posted
Updated 16-Feb-22 20:02pm
v2
Comments
PIEBALDconsult 16-Feb-22 19:54pm    
Have some food and look at it again later. You'll see the problem.

Quote:
The name 'j' does not exist in the current context

C#
using System;


int [,] arr = new int[5,5];
int counter = 0;

for (int i = 0;i < 5; i++){
  for (int j = 0; j < 5; j++){
    ++counter;
    arr[i,j] = (j== 0) ? 0 : counter;
  }
}

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++); // error is here, replace ';' by '{'

    Console.Write($"{arr[i,j]} ");
    Console.Write("\n");
}
 
Share this answer
 
v2
Comments
charles henington 16-Feb-22 20:10pm    
Smart, but also op wants the expected output, that can only be done by moving the ++counter. See my Solution
Nima Nikravan 17-Feb-22 7:14am    
Thanks for the help, now is working! :))
C#
int[,] arr = new int[5, 5];
            int counter = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    arr[i, j] = (j == 0) ? 0 : counter;
                    ++counter;
                }
            }

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write($"{arr[i, j]} ");
                }
                Console.Write("\n");
            }
 
Share this answer
 
v4
Comments
Nima Nikravan 17-Feb-22 7:14am    
Thanks!
charles henington 17-Feb-22 16:26pm    
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
Stolen from @Nelek
Semicolons in C# terminate a statement, so this one:
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++);
                               ^
                               |
    Console.Write($"{arr[i,j]} ");
    Console.Write("\n");
ends the loop, and does not include any other lines in it. If you look at your code you will see that the indentation reflects that: the Console.Write instruction is not indented at all from the for loop above it.

As a result, the variable jdeclared in the loop goes out of scope immediately and is not available in the next line.

This may help you next time you get a compilation error: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]
 
Share this answer
 
Comments
Nima Nikravan 17-Feb-22 7:15am    
Thank you for the tip, i will give a look!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900