C
C#13mo ago
pape

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
Jimmacle
Jimmacle13mo ago
what do you think the first step could be?
pape
papeOP13mo ago
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
Jimmacle
Jimmacle13mo ago
that sounds like a good place to start to me
pape
papeOP13mo ago
yes well what after that? i dont have a clue
Jimmacle
Jimmacle13mo ago
try writing out the code that matches what you've explained so far it seems like you have a few clues already
pape
papeOP13mo ago
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
Jimmacle
Jimmacle13mo ago
exactly
pape
papeOP13mo ago
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
Jimmacle
Jimmacle13mo ago
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#
pape
papeOP13mo ago
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
Jimmacle
Jimmacle13mo ago
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?
pape
papeOP13mo ago
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?
Jimmacle
Jimmacle13mo ago
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
pape
papeOP13mo ago
i don't know how to write that out 😦
Jimmacle
Jimmacle13mo ago
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
pape
papeOP13mo ago
how do you format written code on this server as in message i mean so it looks nice
Jimmacle
Jimmacle13mo ago
$code
MODiX
MODiX13mo ago
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/
pape
papeOP13mo ago
int[] array = { 12, 23, -22, -765, 43, 545, -4, -55, 43, 12, 351, -999, -87 };

int[] arrayPos = {array.Length};
int[] arrayNeg = {array.Length};

for (int i = 0; i < array.Length; i++)
{
foreach (var a in array)
{
if (a < 0) ;
}
}
int[] array = { 12, 23, -22, -765, 43, 545, -4, -55, 43, 12, 351, -999, -87 };

int[] arrayPos = {array.Length};
int[] arrayNeg = {array.Length};

for (int i = 0; i < array.Length; i++)
{
foreach (var a in array)
{
if (a < 0) ;
}
}
okay so this is how i would start i guess
Jimmacle
Jimmacle13mo ago
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?
pape
papeOP13mo ago
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
jcotton42
jcotton4213mo ago
Make them as large as the origin array, or use List<int> instead
pape
papeOP13mo ago
cant use list so yeah array.length is good here?
jcotton42
jcotton4213mo ago
Yeah
pape
papeOP13mo ago
int[] array = { 12, 23, -22, -765, 43, 545, -4, -55, 43, 12, 351, -999, -87 };

int[] arrayPos = {array.Length};
int[] arrayNeg = {array.Length};

for (int i = 0; i < array.Length; i++)
{
if (i > 0)
{

}
}
int[] array = { 12, 23, -22, -765, 43, 545, -4, -55, 43, 12, 351, -999, -87 };

int[] arrayPos = {array.Length};
int[] arrayNeg = {array.Length};

for (int i = 0; i < array.Length; i++)
{
if (i > 0)
{

}
}
am i on the right track?
pape
papeOP13mo ago
yes so how would i write it i cant find online?
Jimmacle
Jimmacle13mo ago
it is very easy to find online, the top 2 results for "c# create new array" show it
jcotton42
jcotton4213mo ago
You couldn't find anything for "c# create array with a certain size"?
Jimmacle
Jimmacle13mo ago
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
pape
papeOP13mo ago
int[] arrayPos = new int[] {array.Length};
int[] arrayPos = new int[] {array.Length};
this good now?
Jimmacle
Jimmacle13mo ago
you didn't even click my link, did you?
jcotton42
jcotton4213mo ago
Literally the first example answers your question @buxna Please read the resources provided to you
pape
papeOP13mo ago
wait im dumb i can just declare them like this? int[] arrayPos;
Jimmacle
Jimmacle13mo ago
no again, please read the documentation i linked specifically the part of the example under // Declare a single-dimensional array of 5 integers.
pape
papeOP13mo ago
oh im really stupid int[] arrayNeg = new int[array.Length]; would it then in my case be like this?
Jimmacle
Jimmacle13mo ago
yep
pape
papeOP13mo ago
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
Want results from more Discord servers?
Add your server