C
C#

Separate one array with positive and negative numbers into two new ones

Separate one array with positive and negative numbers into two new ones

buxna11/21/2023
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.
JJimmacle11/21/2023
what do you think the first step could be?
buxna11/21/2023
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
JJimmacle11/21/2023
that sounds like a good place to start to me
buxna11/21/2023
yes well what after that? i dont have a clue
JJimmacle11/21/2023
try writing out the code that matches what you've explained so far it seems like you have a few clues already
buxna11/21/2023
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
JJimmacle11/21/2023
exactly
buxna11/21/2023
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
JJimmacle11/21/2023
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#
buxna11/21/2023
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
JJimmacle11/21/2023
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?
buxna11/21/2023
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?
JJimmacle11/21/2023
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
buxna11/21/2023
i don't know how to write that out 😦
JJimmacle11/21/2023
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
buxna11/21/2023
how do you format written code on this server as in message i mean so it looks nice
JJimmacle11/21/2023
$code
MMODiX11/21/2023
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/
buxna11/21/2023
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
JJimmacle11/21/2023
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?
buxna11/21/2023
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
Jjcotton4211/21/2023
Make them as large as the origin array, or use List<int> instead
buxna11/21/2023
cant use list so yeah array.length is good here?
Jjcotton4211/21/2023
Yeah
buxna11/21/2023
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?
JJimmacle11/21/2023
buxna11/21/2023
yes so how would i write it i cant find online?
JJimmacle11/21/2023
it is very easy to find online, the top 2 results for "c# create new array" show it
Jjcotton4211/21/2023
You couldn't find anything for "c# create array with a certain size"?
JJimmacle11/21/2023
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
buxna11/21/2023
int[] arrayPos = new int[] {array.Length};
int[] arrayPos = new int[] {array.Length};
this good now?
JJimmacle11/21/2023
you didn't even click my link, did you?
Jjcotton4211/21/2023
Literally the first example answers your question @buxna Please read the resources provided to you
buxna11/21/2023
wait im dumb i can just declare them like this? int[] arrayPos;
JJimmacle11/21/2023
no again, please read the documentation i linked specifically the part of the example under // Declare a single-dimensional array of 5 integers.
buxna11/21/2023
oh im really stupid int[] arrayNeg = new int[array.Length]; would it then in my case be like this?
JJimmacle11/21/2023
yep
buxna11/21/2023
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

Looking for more? Join the community!

C
C#

Separate one array with positive and negative numbers into two new ones

Join Server
Want results from more Discord servers?
Add your server
Recommended Posts
Generic InterfacesI am having trouble implementing a method in my class that I derived from my Generic interface usingManual setup of OpenTelemetry with/without Microsoft.Extensions.Hosting in a Console applicationAfter seeing Aspire I have decided to see how OpenTelemetry, and i first created a console applicatiQuestion regarding learning.. stuff.Might be weirdge for some, but I've never really understood the perception and grasping of learning.[EF Core 8] Many-to-many relationship with payloadHi all, I am trying to create a many-to-many relationship as described in https://learn.microsoft.coDo you know any C# code editors where I can run code?Under 500 MB✅ Getting back main functionRecently after updating to CS8 (didnt update in a long time), and creating a console app there is noCookie Clicker project for schoolso i have a problem i need to fix a bug on a picture. I use mouse down and mouse up events to resize✅ else statement executing regardless of whether I want it to or not, windows form appI've created a simple windows form app to create shapes on a bitmap, I have a function that gives anMaui adb additional parametersI need to test a Maui applicaiton on a physical android device. The app recorded a error, to see whHow to let compiler know that List is not null?Hi community! I have the following extension method:, which checks if the source is not null and if✅ How to Fix MSB6006 yasm.exe exited with code -1073741515 on Cosmos Kernel✅ Issue during pushing a commit to GitHub from VS CodeHi friends, I'm working on a project using `VS Code`, I used `VS Code` to clon that project from my✅ Need help understanding inheritance with constructorsI know about constructors partially (I'm used to Unity's MonoBehaviour so constructors are mostly suHow to get the data of an adapter using MQTTnetThis is the documentation: https://docs.service.sicon.eco/sicon.os/2023.19.x/adapter-protocol-smqtt Converting from a string to a generic method invocationLooking for design patterns to help solve this problem I’m having at work. The situation is that we Auto refresh inside windows formTeam , We are using windows form c#. Is there any library for auto refresh (need to few logic eve✅ Fresh Install of Windows, I'm Getting Weird Errors (NETSDK1/NU1012, and DEP0700)Anyone know what might be happening here in my Maui Blazor Hybrid projects? For the older project object reference not set to instance of object on a static method callso im trying to use an opengl binding package (Veldrid.OpenGLBinding) but, when i call any of the meWeb API model classesI'm coding a webshop using ASP.NET Core Web API for the backend and Next.js for the frontend. I'm deInvalidate JWT-Tokens on logout and password change.I have an ASP.net Web api that handles user interaction. For authentification I use JWT Tokens. I re