Sep 22, 2017 at 5:08am UTC
I'm doing this program that has to read an array of digits of 20, when the user enter -1, thats the end of the array
i.e
54251801 -1(end of array).
also the last number(9) has to be ignore for future operations.
Cheers guys.
Last edited on Sep 25, 2017 at 5:25am UTC
Sep 22, 2017 at 1:17pm UTC
track the size as you add to it. you can do that in the first or last element (you will need to allocate an extra slot for this), or you can add it onto the array via a struct (struct mything{int arr[somesize]; int size;};, or you can use a vector (preferred approach, vectors can grow to fit and track their own size for you).
Sep 23, 2017 at 10:19am UTC
I'm only allow to use array, and the break is not working, what else can i do guys?
Cheers
Sep 23, 2017 at 12:00pm UTC
The problem here:
5425180123456789 -1(end of array).
is that
-1
isn't a
digit . It is certainly an integer. So is
5425180123456789
. You have two integers (one of them very large).
Now if you do the input like this, it becomes more manageable:
5 4 2 5 1 8 0 1 2 3 4 5 6 7 8 9 -1
A modified version of the code posted previously by
Hengry
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int size = 20;
int array[size];
int count;
for (count = 0; count < size; count++)
{
std::cin >> array[count];
if (array[count] == -1)
break ;
}
for (int i = 0; i < count; i++)
{
std::cout << setw(3) << (i+1) << setw(2) << array[i] << "\n" ;
}
}
also the last number(9) has to be ignore for future operations.
Is it to be ignored because it is a '9', or simply because it is the last? If the latter, just subtract 1 from count after the end of the first loop.
Last edited on Sep 23, 2017 at 12:14pm UTC
Sep 23, 2017 at 12:32pm UTC
Hey thanks heaps, i realised i was skipping something.
SOLVED!!