Question on arrays

Feb 27, 2015 at 6:57am
So I've only just begun learning c++ and I was practicing me new learnt skills on some exercises, one of the exercises asked this question :

"Write a program that asks the user to type 10 integers of an array and an integer value V and an index value i between 0 and 9. The program must put the value V at the place i in the array, shifting each element right and dropping off the last element. The program must then write the final array."

The program works as its intended. I would just like to know what happens to the last value which got "pushed aside" Is it still lingering in my program? Or since I declared my array to have 10 values, that last value is "deleted"

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    int numReplace; // New number to be put in array
    int arrayPlace;
    int array1[10];
    int array2[10];

    for (int i = 0; i < 10; ++i)
    {
        cout << "Enter number " << i+1 << ":";
        cin >> array1[i];

    }

      for (int i = 0; i < 10; ++i)
    {
       array2[i] = array1[i];

    }

    cout << "Enter the new number you want to place in the array:";
    cin >> numReplace;

    cout << "Enter a place from 0-9 to insert the array:";
    cin >>arrayPlace;


    for (int i = 0; i < 10; ++i)
    {

        if(arrayPlace == i)
        {
            array1[i] = numReplace;

          for(; i < 10; ++i)
        {
            array1[i+1] = array2[i];

        }


        }
    }


    for(int i = 0; i < 10; ++i)

    {

        cout << array1[i] << " ";
    }


    system("PAUSE");
    return 0;
}
Feb 27, 2015 at 7:34am
it isnt deleted.
the local array is only allocated for 40 bytes ( or 20 bytes in 64bit ).
notice that the program crushed when you terminated the program.
the last value of that array which is 4 bytes is stored in the next memory,which you dont own, after 40 bytes

Feb 27, 2015 at 3:53pm
I'm not seeing where the program crushes. I'm assuming this is a bad thing though. What should I do to fix it.
Feb 27, 2015 at 5:27pm
On line 43, what happens when i == 9?
Feb 27, 2015 at 6:38pm
What you have written, doesn't add an element. You are re writing what is there.
If you want to add elements, to make the array bigger, use vectors.
Feb 28, 2015 at 2:10am
I can see why line 43 doesn't make sense, if i == 9, then I'm taking that value and putting it into the index 10, which doesn't exists because the array only has 10 spaces. I think what I posted should make more sense now.


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    int numReplace; // New number to be put in array
    int arrayPlace;
    int array1[10];
    int array2[10];

    for (int i = 0; i < 10; ++i)
    {
        cout << "Enter number " << i+1 << ":";
        cin >> array1[i];

    }

      for (int i = 0; i < 10; ++i)
    {
       array2[i] = array1[i];

    }

    cout << "Enter the new number you want to place in the array:";
    cin >> numReplace;

    cout << "Enter a place from 0-9 to insert the array:";
    cin >>arrayPlace;


    for (int i = 0; i < 10; ++i)
    {

        if(arrayPlace == i)
        {
            array1[i] = numReplace;

          for(int j = 9; j > i; --j)
        {
            array1[j] = array2[j-1];

        }


        }
    }


    for(int i = 0; i < 10; ++i)

    {

        cout << array1[i] << " ";
    }




    system("PAUSE");
    return 0;
}
Feb 28, 2015 at 5:51am
It crushes in vs12 when terminated.
Feb 28, 2015 at 6:13am
Lines 34 through 49 need more thinking.

First, the only time you do anything is if arrayPlace == i (line 37). So why are you looping for i = 0..9 (line 34)?

Next, given your array of numbers:

+---+---+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|
+---+---+---+---+---+---+---+---+---+---+
                  ^arrayPlace

If the first thing you do, on line 39, is replace the number at i==arrayPlace, then you wind up with:

+---+---+---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | -3| 6 | 7 | 8 | 9 | 10|
+---+---+---+---+---+---+---+---+---+---+
                  ^arrayPlace

You've lost a number!

You should shift the numbers first.

The only remaining issue is that the unnecessary loop on line 32 did have the side effect of protecting you against your user entering a bad arrayPlace. You could change line 34 to read something like:

if (arrayPlace >= 0 && arrayPlace < 10)

That way nothing happens if the user enters an invalid number.



One final thing to notice. Line 30 disagrees with line 16:

Enter number 1:1
Enter number 2:2
Enter number 3:3
Enter number 4:4
Enter number 5:5
Enter number 6:6
Enter number 7:7
Enter number 8:8
Enter number 9:9
Enter number 10:10
Enter the new number you want to place in the array:-3
Enter a place from 0-9 to insert the array:

/me "What? The elements are listed as 1-10."

Keep your language consistent with the user. Let him enter a number in 1-10. Once you have that, just subtract one from whatever he gave you.

Pretty good otherwise! Hope this helps.


[edit] BTW, a program "crashes". It doesn't crush. Tin cans and submarines and boxes get crushed. Programs drive off the road into forests full of trees and bushes and angry bears and little biting bugs.

:O)
Last edited on Feb 28, 2015 at 6:15am
Mar 1, 2015 at 5:49am
I think I got it now. Thanks for all the help everyone. This is what I ended up with:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	int numReplace; // New number to be put in array
	int arrayPlace;
	int array1[10];
	int array2[10];

	for (int i = 0; i < 10; ++i)
	{
		cout << "Enter number " << i + 1 << ":";
		cin >> array1[i];

	}

	for (int i = 0; i < 10; ++i)
	{
		array2[i] = array1[i];

	}

	cout << "Enter the new number you want to place in the array:";
	cin >> numReplace;

	cout << "Enter a place from 1-10 to insert the array:";
	cin >> arrayPlace;
	arrayPlace = arrayPlace - 1;

	

		if (arrayPlace >= 0 && arrayPlace <10)
		{
			

			for (int j = 9; j > arrayPlace; --j)
			{
				array1[j] = array2[j - 1];

			}
			array1[arrayPlace] = numReplace;


		}
	


	for (int i = 0; i < 10; ++i)

	{

		cout << array1[i] << " ";
	}




	system("PAUSE");
	return 0;
}
Topic archived. No new replies allowed.