← Back to all posts
I need help with arrays! :/

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 (184) [earned 5 cycles]
themaka (184)

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

Wo1ves1n0 (5)

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

#include <iostream>
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;``

}

SPQR (533)

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

SPQR (533)

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

Wo1ves1n0 (5)

@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.

AzureScripts (166)

I did something similar to this here:

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

I hope this helps a little bit

TheDrone7 (1316)

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.

ArchieMaclean (830)

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

``````first = list[0]
last = list[-1]``````
TheDrone7 (1316)

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

SPQR (533)

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

SPQR (533)

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

TheDrone7 (1316)

@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.

SPQR (533)

@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.

TheDrone7 (1316)

@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.

ArchieMaclean (830)

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

ArchieMaclean (830)

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

Wo1ves1n0 (5)

@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.

NEILSENBENJAMIN (25)

@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.)