-2

I'm trying to figure out how to assign values to an array while a for loop is happening. I've tried displaying one of the elements in the array but it always ends up with 0

using System;

namespace Assignment4
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numArray = new int[48]; // 48 elements
            int total = 1;   // trying to get odd numbers to fill for the elements         
            int index;
            for (index = 0; index <= numArray.Length; index++) // for loop to assign array
            {
                total = total + 2;
                Console.WriteLine("#{0} current number is {1} ", index + 1, total); // double checking the index thing
            }
            Console.WriteLine(numArray[34]); // checking if the specified array will display the number in the list
        }
    }
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
bruh1000
  • 7
  • 4
  • 1
    You are not assigning values to the array elements in the for loop. – Chetan Feb 11 '21 at 07:34
  • The value is 0, because that's the default value for an `int[]` element, and you haven't bothered to change the value of any element. See duplicate. – Peter Duniho Feb 11 '21 at 08:10

3 Answers3

2

index < numArray.Length // 1st change else it will throw an exception at numArray[48] = total if it was <=

numArray[index] = total // 2nd change Assign it to the array

int[] numArray = new int[48]; // 48 elements
int total = 1; // trying to get odd numbers to fill for the elements         
int index;

// Changed <= to <
for (index = 0; index < numArray.Length; index++) // for loop to assign array

{
    total = total + 2;
    numArray[index] = total; // This is where you assign the values.
    Console.WriteLine("#{0} current number is {1} ", index + 1, total); // double checking the index thing
}

Console.WriteLine(numArray[34]); // checking if the specified array will display the number in the list
hendrixchord
  • 4,662
  • 4
  • 25
  • 39
1

the way to go is using the assign operator:

numArray[i] = y;

this asigns the value y to the cell i of the array numArray

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You are missing assignment to an array. Let's see the below code. Here we are looping inside for for finding odd numbers from 0 to 100, and if found, storing in declared array, index wise.

using System;

namespace Assignment4
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int upperLimit=100;//adding this to loop upto
            int[] numArray = new int[48]; // 48 elements
            int total = 1;   // trying to get odd numbers to fill for the elements         
            int index,i=0;
            for(index = 0; index <= upperLimit; index++) // for loop to assign array, we will go upto 100 numbers
            {
                total = total + 2;
                Console.WriteLine("#{0} current number is {1} ", index+1, index+1); // double checking the index thing
                if(index%2==1){//checking if that one is an odd number
                    Console.WriteLine("{0} is odd number",index);
                    numArray[i]=index;
                    i++;
                }
            }
            Console.WriteLine(numArray[34]); // checking if the specified array will display the number in the list
        }
    }
}
Hemal
  • 3,682
  • 1
  • 23
  • 54