Skip to content
← Back to Community
I need help with arrays! :/
Profile icon
Wo1ves1n0

So I've got a code where the player inputs the amount of numbers for the list then will have to input the numbers for list they decided it to be. However, i manage to get the sum to add the total. It's just finding the lowest number to the largest number And I can't find anything about it.

Answered by themaka [earned 5 cycles]
View Answer
Voters
Profile icon
RayyanZaidi
Profile icon
PythonCoder100
Profile icon
Wo1ves1n0
Comments
hotnewtop
Profile icon
themaka

Hello,
It's easier to help if we can see your repl -- it sometimes puts your question into context for us.

Profile icon
Wo1ves1n0

@themaka /*
Computational Thinking
Assigment 3 template
"Torren Walker"
*/

#include
using namespace std; // cout instead of std::

int main() //Main body for the code
{

int sum=0; //Sum for where to start 0 = starting point
int length = 0; //integer used to create the length of the array
cout << "How many numbers are you checking for?: ";
cin >> length; // the amount of numbers for the list

int MyList[length]; //Array is created using the value of length

for (int i=0; i<length; i++) //For loop to enter values into the array
{
cout << "Enter number " << i+1 << ": ";
cin >> MyList[i];
}

cout << endl << "Printing Array"<< endl << "--------------" << endl; // text

for (int i=0; i<length; i++) //For loop to enter values into the array
{
sum+=MyList[i]; // Adds the list that the user as given the integer for
cout << MyList[i] << endl; // puts the list together of what the user has entered.
}
cout << "--------------" << endl << "Adding your list of numbers" << endl;//Caption
cout << "Total: " << sum << endl; // the total for the list added

return 0;

}

Profile icon
SPQR

You can't have an array of variable length. Try using <vector>.

Profile icon
Wo1ves1n0

@SPQR Why?

Profile icon
SPQR

@Wo1ves1n0 because you can resize vectors. Arrays have to be declared at a constant size, otherwise you'll get an error.

Profile icon
Wo1ves1n0

@SPQR Yeah, But the user can input the amount of numbers for the list then enter the numbers for each number in the list then I've done the total. Just need to get it to do the highest and lowest number in the list.

Profile icon
EvanTeh

idk too

Profile icon
AzureScripts

I did something similar to this here:

https://repl.it/@AzureScripts/Lowest-Int

I hope this helps a little bit

Profile icon
TheDrone7

I'm assuming you're using python? If yes, then you can simply do list.sort() to sort all values in ascending order. Then just get the first and last value from the list and there you have it, there are also similar methods in different languages.

Profile icon
ArchieMaclean

@TheDrone7 To get the first and last values from a list in python do

first = list[0] last = list[-1]
Profile icon
TheDrone7

@ArchieMaclean well ik but helping means to show the way not leading the way. Hope you understand.

Profile icon
SPQR

@TheDrone7 This is C++, you can tell from some of the syntax

Profile icon
SPQR

@TheDrone7 wait he hadnt posted his code when u said this nvm

Profile icon
TheDrone7

@SPQR I know, he hadn't posted the code when I replied, so I automatically assumed that it was python code. But the technique I offered wasn't only limited to python, he could still sort the list and get the first and last element from the sorted array or vector.

Profile icon
SPQR

@TheDrone7 I wasn't trying to say that you were unhelpful or anything, and that's a perfectly reasonable assumption to make. I just wanted to let you know what language this is. As far as I know, though, there isn't any native way to sort an array in C++. However, I could be wrong.

Profile icon
TheDrone7

@SPQR you're not wrong as far as I know and I was just saying that you were right, so no need to apologise. And thank you.

Profile icon
ArchieMaclean

What language are you using? Do you have a link to a repl?

Profile icon
Wo1ves1n0
Profile icon
ArchieMaclean

@Wo1ves1n0 I don't know C++, but does this stackoverflow question help?

Profile icon
Wo1ves1n0

@ArchieMaclean Not really. But thank you! However, I'm trying to look for a code where I can find the lowest and the highest integer that the user has inserted in the array themselves, not the ones that only you create. If that makes sense.

Profile icon
NEILSENBENJAMIN

@Wo1ves1n0 I don't know C++, but I could suggest that you constantly update an integer minimum and integer maximum as you get the numbers from the user. (Also, you don't really even need an array to do this, just do everything in real time.)