-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab2.cpp
More file actions
168 lines (130 loc) · 4.8 KB
/
lab2.cpp
File metadata and controls
168 lines (130 loc) · 4.8 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Working with arrays and functions
// Author: Faiza Khan
// Last modified on: January 31, 2018
#include <iostream>
using namespace std;
const int CAPACITY=20;
//TODO: Declare a function that displays the content of an int array,
//both the array and the size of array will be passed as parameters to the function
int display(int[], int);
//TODO: Declare a function that searches for a value in a given int array, it returns
//-1 if the value does not occur in the array; if the value occurs in the array,
// the function returns the index of its first occurance.
int search(int array[],int size, int value);
//TODO: Declare a function that deletes the element stored in a specified position from the array
int delete_value(int array[], int& size, int position);
/*
inserts a value into a given position in an array of int
if the given position is occupied, that number and its subsequent numbers will
shifted to the right of the array.
@param array: the int array that the value is to be inserted into
@param array_size: the current size of the array. Upon successful
insertion, the array_size will be increased by 1
@param value: the value to be inserted
@param index: the value is supposed to be inserted into the given index
@return -1 if the array is already full, cannot insert a new value
otherwise, return the index of the new value in the array
@precondition: index is less than array_size
*/
int insert_value(int array[], int& size, int position, int value);
//TODO: Declare a function that swaps two vaues. Remember to use call-by-reference.
void swap_values(int array[], int size, int value);
// TODO: Declare a function that reverses an array.
int reverse_values(int array[], int& size);
int main()
{
// As the NumArray can be partially filled, we use variable NumArraySize to keep track of how many numbers
// have been stored in the array.
int NumArray[CAPACITY]; // a int array with a given capacity
int NumArraySize=0; // the array is initially empty, i.e., contains 0 elements
//1. TODO: Prompt the user to enter a sequence of integer values, separated by space, and ended with -1,
// and store the values in the array
// Display the array afterwards
cout << "Enter up to " << CAPACITY << " values or -1 to exit" << endl;
int next;
do
{
cin >> next;
if (next!=-1) NumArray[NumArraySize++]=next;
} while (next!=-1 && NumArraySize <= CAPACITY);
display(NumArray, NumArraySize);
// 2. TODO: Call your function to search for value 50.
int index=search(NumArray, NumArraySize, 50);
cout << index << endl;
if (index==-1)
cout << "50 was not found" << endl;
else
cout << "50 was found at position " <<index << endl;
// 3. Call your function to append a value (entered by the user) to the array
// Display the content of the array afterwards
display(NumArray, NumArraySize);
// 4. Call your function to insert a value (entered by the user) to a given position (entered by the user)
// of the array
// Display the content of the array afterwards
int value, position;
cout << "What value do you want to insert?" << endl;
cin >> value;
cout << "Which position would you like to insert this value? "<< endl;
cin >> position;
insert_value(NumArray, NumArraySize, position, value);
// 5. Call your function to delete the values stored in position 0 and 2
display(NumArray, NumArraySize);
delete_value(NumArray, NumArraySize, 2);
display(NumArray, NumArraySize);
delete_value(NumArray, NumArraySize, 0);
// Display the content of the array after the deletion.
display(NumArray, NumArraySize);
// 6. Call your function to reverse the array.
reverse_values(NumArray, NumArraySize);
display(NumArray, NumArraySize);
}
// TODO: implement all functions (after declaring them in main, I wrote the code for each below)
int display(int array[], int size){
for (int i=0; i<size; i++)
{
cout << array[i]<< ", ";
}
cout << endl;
}
int delete_value(int array[], int& size, int position)
{
for (int i=position; i<size; i++)
{
array[i]=array[i+1];
}
size=size-1;
}
int insert_value(int array[], int& size, int position, int value)
{
if (position +1 > size)
cout << "Invalid position!" << endl;
for(int i = size; i >= position; i--)
{
array[i+1] = array[i];
}
array[position]=value;
size++;
}
void swap_values(int array[], int size, int a, int b)
{
int temp = array[a];
array[a]=array[b];
array[b]=temp;
}
int reverse_values(int array[], int& size)
{
int halfSize = size/2;
for (int i = 0; i < halfSize; i++)
{
swap_values(array, size, i, size-i-1);
}
}
int search(int array[], int size, int value)
{
for (int i=0; i<size; i++)
{
if (array[i]==value)
return i;
}
return -1;
}