Separate one array with positive and negative numbers into two new ones
One should have only positive numbers of the original one and the other only the negative ones.
int[] array = { 12, 23, -22, -765, 43, 545, -4, -55, 43, 12, 351, -999, -87 };
i got this as an assignment, but i don't know how to do it, i guess they want me to use the for loop but i don't know how, we also didn't learn any other methods so i think this has to be as simple as it can be.
38 Replies
what do you think the first step could be?
I literally don't know, i can write with the forreach loop and get all the negatives and positives sorted, but i don't know how to make new arrays
i guess i would declare 2 new arrays
one for pos one for neg
that sounds like a good place to start to me
yes well what after that?
i dont have a clue
try writing out the code that matches what you've explained so far
it seems like you have a few clues already
so i would use a for loop that checks every number if its greater or smaller than zero, and if its for example greater then it would put it in the new array that i declared
exactly
thats what im thinking
but im stuck
i dont know what to write, how would it check if its greater than zero, and what would be the proper syntax for adding it to the already declared array
i can write the basic for loop but beyond that im stuck
i can write a for loop, then maybe nest a forreach in it, that works, but then what is the syntax for adding them to a new array
have you tried searching to find the syntax needed to express what you want? it sounds like you've already solved the problem, you just need to find how to write it in C#
yesss i tried, but there are always some methods used, with which im familiar with because i've been learning C# on and off for a better part of a year, but i started this new course for the first time and i dont think they want me to use some of those methods that werent learned in the course
without knowing those restrictions i can't give you the right answers
has the class not already taught you how to do the things you need to do to complete the assignment?
it has, but only like the most basic
all the loops, but not all the methods
For example, the Array.Copy method has been taught
how would you do it?
in addition to what you've already said you only need to add a couple counters to track the current index in each output array and an if statement using a comparison operator
there's no advanced syntax or knowledge of methods needed
i don't know how to write that out 😦
what specifically do you not know how to write?
if it's more than one thing just pick one to start with
it might help to write the actual code and just put comments in places you're stuck
that will help me figure out where you're stuck at least
how do you format written code on this server
as in message i mean so it looks nice
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/
okay so this is how i would start i guess
so a couple things
1. that's not the correct syntax to create a new array, you should look that up
2. why do you have 2 loops?
i guess, but how do declare new arrays if i dont know their lenght
i could manually count the numbers but i dont think that is the right thing
also for 2. i dont know any other way
Make them as large as the origin array, or use List<int> instead
cant use list
so yeah array.length is good here?
Yeah
am i on the right track?
closer, but your array syntax is still incorrect
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/arrays
yes so how would i write it i cant find online?
it is very easy to find online, the top 2 results for "c# create new array" show it
You couldn't find anything for "c# create array with a certain size"?
the reason i'm trying to get you to find it yourself is because lots of programming involves looking up how to do things
so it's a good skill to develop early
this good now?
you didn't even click my link, did you?
Literally the first example answers your question @buxna
Please read the resources provided to you
wait im dumb
i can just declare them
like this?
int[] arrayPos;
no
again, please read the documentation i linked
specifically the part of the example under
// Declare a single-dimensional array of 5 integers.
oh im really stupid
int[] arrayNeg = new int[array.Length]; would it then in my case be like this?
yep
because i cant just write a number
thank you i was being stupid
so how do i track them now
and put the positives into the new array