Jump to content

int to Array C# help

Redliquid

hi friends, I'm wondering about way to put pre declared int in to a Array

 

 

 

        static void Main(string[] args)
        {
 
            int a = 0;
 
            int[] arrayStore;
 
            for (int i = 0; i < 10; i++)
            {
                arrayStore[]//??????????????? xD
 
                a += 1;
 
            }
 
        }
 
 
I'm trying to get int a in to the Array,
Increase a by 1 and slap the new a in to the array in the next position so it becomes 

int[] arrayStore{1,2,3,4,5,6,7,8,9}

How'd i write that though?

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe like that?

EDIT: also, please, format your code according to the hungarian convention.

int a = 0;for (int i = 0; i < 10; i++) {   a++;   arrayStore[i] = a;}
Link to comment
Share on other sites

Link to post
Share on other sites

nt[] arrayStore;

 

 

That's not how you declare an array in c++.

 

You need to specify how long it is:

int arrayStore[x];

where x is the desired size. If you want to add stuf as you go you'll have to use a vector<>

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

That's not how you declare an array in c++.

 

You need to specify how long it is:

int arrayStore[x];

where x is the desired size. If you want to add stuf as you go you'll have to use a vector<>

Thought it was something like

int[] newArray = new int[x];

or coding on many langs at the same time's killing me...

Link to comment
Share on other sites

Link to post
Share on other sites

 

Maybe like that?

int a = 0;for (int i = 0; i < 10; i++) {   a++;   arrayStore[i] = a;}

haha if a is 0 then why not use i at the first place

 

 

declare like this:

int[] newArray = new int[9];

remember that while arrays start with array[0] their length isnt the maximal number but the amount of places including array[0]

for (int i = 0; i < 9; i++) {arrayStore[i] = i+1;}

or

for (int i = 1; i < 10; i++) {arrayStore[i-1] = i;}

http://www.tutorialspoint.com/csharp/csharp_arrays.htm

 

for more information

Link to comment
Share on other sites

Link to post
Share on other sites

That's not how you declare an array in c++.

 

You need to specify how long it is:

int arrayStore[x];

where x is the desired size. If you want to add stuf as you go you'll have to use a vector<>

What's a vector?

I wanted the array to change size depending on how many nrs i put it

Also how do i print the whole array? :)

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

What's a vector?

I wanted the array to change size depending on how many nrs i put it

Also how do i print the whole array? :)

 

A vector is similar to an array, but it's dynamic. It's superior in many ways. http://www.cplusplus.com/reference/vector/vector/

Arrays can't change size after they are declared. What you can do is declare an array that is certainly larger than what you need and only fill the first x spots.

 

To print it use a for loop with i<(number of elements) and cout << array inside it.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

What's a vector?

I wanted the array to change size depending on how many nrs i put it

Also how do i print the whole array? :)

if you have no variable set for its length you can use array.length to get it in a for loop

Link to comment
Share on other sites

Link to post
Share on other sites

 

A vector is similar to an array, but it's dynamic. It's superior in many ways. http://www.cplusplus.com/reference/vector/vector/

Arrays can't change size after they are declared. What you can do is declare an array that is certainly larger than what you need and only fill the first x spots.

 

To print it use a for loop with i<(number of elements) and cout << array inside it.

That helped alot ^^

Gonna learn more about Vectors now!

Thanks friends  :D

Vectors -> Lists?

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

Vectors -> Lists?

 

 

Yes, in C# we use the List<T> class where T is the type you'll be using.

List<int>List<double>List<string>List<MyCustomClass>// etc
Link to comment
Share on other sites

Link to post
Share on other sites

 

Yes, in C# we use the List<T> class where T is the type you'll be using.

List<int>List<double>List<string>List<MyCustomClass>// etc

I see :)

That's what i was using before but i thought Arrays were more efficient when it comes to processing power ^^

 

Redliquid~

Link to comment
Share on other sites

Link to post
Share on other sites

Vectors -> Lists?

 

Sort of. I'm given to understand that it's not quite the same thing (I think multidimensional vectors aren't as flexible as lists) but frankly I'm still not informed enough to know ^^ after all I'm just a uni student.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

 

That's what i was using before but i thought Arrays were more efficient when it comes to processing power ^^

 

This post provides some information. In general, if you need a dynamic array (ie: an array that changes in size) it's best to just use a list. A List is using an array internally and automatically handles specific details for you like resizing.

 

Note that List<T> and a LinkedList<T> are different. Each is better at certain tasks due to the data structure differences.

Link to comment
Share on other sites

Link to post
Share on other sites

Sort of. I'm given to understand that it's not quite the same thing (I think multidimensional vectors aren't as flexible as lists) but frankly I'm still not informed enough to know ^^ after all I'm just a uni student.

 

I believe the C++ vector and C# List are pretty much equivalent. They may be implemented a little differently or provide different APIs to interact with them, but I believe they implement the same underlying data structure, a dynamic array. I can't imagine multidimensional work would change their relationship much but I don't know enough about each implementation to know for sure.

Link to comment
Share on other sites

Link to post
Share on other sites

I believe the C++ vector and C# List are pretty much equivalent. They may be implemented a little differently or provide different APIs to interact with them, but I believe they implement the same underlying data structure, a dynamic array. I can't imagine multidimensional work would change their relationship much but I don't know enough about each implementation to know for sure.

 

I was thinking of python lists actually, I don't know much about c# to be completely honest.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

I was thinking of python lists actually, I don't know much about c# to be completely honest.

 

I believe the default list in Python is also a dynamic array so it should also be similar to the C++ vector and C# List.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×