Dynamic array

Pages: 12
closed account (23q2T05o)
Okay but here the input is 14 16 18 20 22
and there its like 14 16 18 20 11
14 16 18 10 10
14 16 9 9 10
closed account (23q2T05o)
And I dont want to use 0, because the use has to input the elements in the array so it would be a problem
14 16 9 9 10 10 11 11
That's what I get. What do you want mr vladimorova?

You don't have to use 0's. You're creating a dynamic array so adding the extra 0's is done automatically if you make the simple adjustment to the array size. I'm not doing that because I have hard-coded the array because I am not interested in typing numbers in repeatedly while I test my code.

I think you need to take a break and then get back into closely working through the problem.
closed account (23q2T05o)
I mena when I input 10 12 14 16 18 and it gives me 10 12 14 16 9 and then 10 12 14 8 8 and its wrong, it should be in the first place 10 12 14 16 18 then 10 12 14 16 9 9 and then 10 12 14 8 8 9 9
And I get the following after 3 trials:

Biggest = 18
10 12 14 16 9 9 0 0 
Biggest = 16
10 12 14 8 8 9 9 0 
Biggest = 14
10 12 7 7 8 8 9 9 
Program ended with exit code: 0 


if I change to trial = 2, I get:


Biggest = 18
10 12 14 16 9 9 0 
Biggest = 16
10 12 14 8 8 9 9 
Program ended with exit code: 0


Whoopee! That's the same as what you get! What a miracle!

You're printing out your input as one trial. I'm not. take your pick :)

@izlezotfilmabrat how many times you have to repeat this process on your array? Because, depending on the numbers stored on it, after N-times all of they will become 0...

(Sorry if you already have answered this question... I missed it.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int main()
{
   list<int> p{ 10, 12, 14, 16, 18 };

   for ( int i = 1; i <= 3; i++ )
   {
      auto it = max_element( p.begin(), p.end() );
      auto value = *it, half = value / 2;
      *it = half;
      p.insert( it, value - half );
      for ( auto i : p ) cout << i << ' ';
      cout << '\n';
   }
}


10 12 14 16 9 9 
10 12 14 8 8 9 9 
10 12 7 7 8 8 9 9 





It's all miles easier in Fortran:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
program fun
   implicit none
   integer, allocatable :: p(:)
   integer i, pos, half

   p = [ 10, 12, 14, 16, 18 ]

   do i = 1, 3
      pos = maxloc( p, 1 )
      half = p(pos) / 2
      p = [ p(:pos-1), p(pos) - half, half, p(pos+1:) ]
      write( *, "( *( i0, 1x ) )" ) p
   end do

end program fun

https://onlinegdb.com/Bkcc4iMyU





Or python:
1
2
3
4
5
6
7
import numpy as np
p = np.array( [ 10, 12, 14, 16, 18 ], dtype="i4" )
for i in range( 3 ):
   pos = p.argmax()
   half = p[pos] // 2
   p = np.hstack( ( p[:pos], [ p[pos] - half, half ], p[pos+1:] ) )
   print( p )

https://onlinegdb.com/Sy4hq-N1L




Just why does C++ make array operations so difficult?
Last edited on
@izvestianewname, yes you’re right, given enough trials ( and 0’s ) the array will end up full of 0’s. That might be the reason for the 1024 limit on input.

It's all miles easier in Fortran

Yes, if you know Fortran. Less so, if you know C++ but no Fortran. Hard to say, if you know neither.

@againtry:
You are confusing with your 0's.

I did mention:
Either way, your array has to be larger than the input data.
1
2
3
size = ...
numa = ...
capacity = size + numa;

That is an idea that the std::vector uses; it allocates memory (an array) for capacity but does not have more than size "real elements".
Vector has both "sizes". The "unused elements" are not 0's; they remain undefined until taken into use.

Use of that idea "hides the 0's".


Insert is cheap with std::list and easy with std::vector. One part of this homework is apparently to understand the logic for "Insert a value into an array"
OP and I weren't confused and that's what counts for me.

The real problem with the zeroes is elsewhere with some sequences of numbers and large numbers of trials, an exercise in itself, but one which I can't be bothered delving into

I did rather like the walk down memory lane (for me) with lastchance's FORTRAN solution and his ability to graciously and quietly accept OP's (inevitable) unstated verdict on sticking with C++ - neither of us would blame him.

5 DO 10
...
10 CONTINUE.

It's good to see FORTRAN has progressed beyond that for a loop.
@izlezotfilmabrat,
What was your intention if the largest element of the array was an odd number?
Say, { 2,3,4,5}
or even
{0,1}
Topic archived. No new replies allowed.
Pages: 12