I almost got it but im having trouble with the finishing touch. i want to create a method that subtracts 3 from 3 numbers inputted by the user and show them at the end of the program. it tells me that x is unassigned but when i declare it inside the method it tells me there is a 'parent' use of the variable, whats going wrong here.......any help or tips would be soo appreciated. thank you in advance.
Here is what I have so far.......
class Program
{
static void Main(string[] args)
{
int x;
int []a = new int[3];
for (x = 0; x < 3; x++)
{
Console.Write("enter number = "+ x);
a[x] = Int32.Parse(Console.ReadLine());
}
method(a);
}
static void method(int[] a)
{
for (int x = x - 3; x < 3; x++) //****my problem is here...where i put "int x = x - 3" // the program tells me that "use of unassigned local // variable x" what does that mean? when i try to // declare it inside the method it gives me
// more errors, why? and how do i fix it?
{
Console.Writeline("first number subtracted by 3 ~~> " + x);
}
}
}
}
Loading
A XPosted Sep 4, 2013, 12:03 PM
mohan rajPosted Sep 4, 2013, 4:39 AM
Try this
Sanjeeb LenkaPosted Sep 4, 2013, 3:02 AM
try this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfExample
{
class substractArray
{
static void Main(string[] args)
{
int[] a = new int[3];
for (int x = 0; x < 3; x++)
{
Console.Write("enter number = " + x + " :");
a[x] = Int32.Parse(Console.ReadLine());
}
method(a);
}
static void method(int[] a)
{
Console.WriteLine("original array");
foreach (int element in a)
{
Console.WriteLine(element);
}
Console.WriteLine("After subsctract by 3 array:");
foreach (int element in a)
{
Console.WriteLine(element - 3);
}
Console.ReadLine();
}
}
}
Kunal VaishyaPosted Sep 4, 2013, 2:51 AM
class Program
{
static void Main(string[] args)
{
int x;
int []a = new int[3];
for (x = 0; x <= 3; x++)
{
Console.Write("enter number = "+ x);
a[x] = Int32.Parse(Console.ReadLine());
}
method(a);
}
static void method(int[] a)
{
for (int x = x - 3; x <= 3; x++)
{
Console.Writeline("first number subtracted by 3 ~~> " + x);
}
}
}
}