Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create an array of integers and then initialize each of them.
The initialisation must be done by the user of the program.

fter that, I want to save the elements of the array in a textfile and manipulate them there.
The string in the textfile must then be saved in a new array which can be printed on the console.

This is a Console Application

[edit]Tags only - OriginalGriff[/edit]
Posted
Updated 13-Mar-11 23:21pm
v3
Comments
Nuri Ismail 14-Mar-11 5:17am    
And what have you tried so far? What is your specific problem? Where are the relevant parts of your code? If you have answers to these questions, please update your original post with these answers.

P.S. Please do not expect from people here to do your assignments. First you have to try to solve your problem by writing some code. If you think that you are not ready for this, then pick up a good programming book... Come back here and ask a question when you face some specific problems. If you do this I guarantee you that you will get a lot of help here.
Dalek Dave 14-Mar-11 5:21am    
Edited for Spelling, Grammar and Readability.

Well, it's your homework, so I'm not going to give you the full code. Here are some pointers though:

1) Get the number of numbers the user is going to enter. If you don't know this, you don't know how big an array to allocate.
You will need: Console.ReadLine, int.Parse or int.TryParse - Google for them appending "MSDN" on the end.
2) Allocate the array.
int[] myArray = new int[numberOfNumbersTheUSerWillEnter];

3) You will need a loop to get the numbers.
for (int i = 0; i < numberOfNumbersTheUSerWillEnter; i++)

4) In the loop, read a number from the user as you did in (1) above, and stuff it into the appropriate array element:
myArray[i] = numberIJustGotFromTheUser;

5) Then use a second loop to write the numbers to a file.
6) Then read the numbers form the file, and print them to the console.

If you have a specific problem with any of this, feel free to ask. But don't expect us to write it for you - it's your homework, not ours!
 
Share this answer
 
Comments
Nuri Ismail 14-Mar-11 5:19am    
Good answer. My 5
Dalek Dave 14-Mar-11 5:22am    
Spot on Griff, take 5
Manfred Rudolf Bihy 14-Mar-11 5:24am    
Good one! 5+
Sergey Alexandrovich Kryukov 14-Mar-11 13:54pm    
Agree, a 5.
--SA
Try to use List class:

XML
List<int> list = new List<int>();
           list.Add(777);
 
Share this answer
 
You can try this too,


Console.WriteLine("Please enter the number of Entries");
int noOfEntries = int.Parse(Console.ReadLine());
int[] numbers=new int[noOfEntries];
Console.WriteLine("Please Enter the numbers");

for(int i= 0;i<noofentries;i++){>
   numbers[i] = int.Parse(Console.ReadLine());
}

for(int j=0;j<numbers.length;j++){>
   File.AppendAllText("textfile.txt",numbers[j]+Environment.NewLine);
}


Then to read it back into an array,
You can do something like this
int[] readBack = File.ReadAllLines("filename.txt")
              .Select(s=>int.Parse(s)).ToArray();
 
Share this answer
 
v2

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