-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays_lesson5.cpp
140 lines (117 loc) · 3.23 KB
/
arrays_lesson5.cpp
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
//Notes-1:
/*
C++ arrays can be declared as:
variableType arrayName [ ] = {variables to be stored in the array};
or as:
variableType arrayName[array size]
*/
//Notes-2:
/*
We can access the values in an array by identifying the specific index.
variableType arrayName[ index number ]
*/
//Example
/*Goal: Practice array manipulation in C++.
**The user will input 40 integers.
**Put them into an array. Then print the array in the order the numbers were
**entered. Then print in reverse order. Then sort the array in ascending order
**and print it.
*/
#include <iostream>
#include <stdio.h>
int main()
{
int userInput[40];
//Enter the numbers into an array called userInput
for(int i = 0; i < 40; i++)
{
scanf("%d", &userInput[i]);
}
//print the array
std::cout<<"\nThe array\n";
for(int i = 0; i < 40; i++)
{
std::cout << userInput[i] <<" ";
}
//print the array in reverse order
std::cout<<"\n\nThe array in reverse order\n";
for(int i = 39; i >= 0; i--)
{
std::cout << userInput[i] <<" ";
}
//sorting the array
for(int i = 0; i < 40; i++)
{
for(int j = 0; j < 39 - i; j++)
{
if(userInput[j] > userInput[j + 1])
{
int temp;
temp=userInput[j];
userInput[j]=userInput[j + 1];
userInput[j + 1]=temp;
}
}
}
std::cout<<"\n\nThe array sorted\n";
for(int i = 0; i< 40; i++)
{
std::cout << userInput[i] <<" ";
}
return 0;
}
//Example-2:
/*Goal: practice searching an array in C++
**There is an array of integers. The length of the arrays to be searched
**varies. A user will enter a postive integer and the program will search the array.
**If the value is in the array, the program will return the location of
**the element. If the value is not in the array, the user will be notified
**that the value is not in the array. To exit the program the user will enter -1.
*/
#include <iostream>
#include <stdio.h>
int main()
{
int searchKey = 0;
int searchArray[10] = {324,4567,6789,5421345,7,65,8965,12,342,485};
int location = 0;
while(1)
{
std::cout<<"Enter an integer ('-1' to quit): ";
scanf("%d", &searchKey);
std::cout<< searchKey<<"\n";
if(searchKey == -1)
{
break;
}
for(int i = 0; i < 10; i++)
{
if(searchKey == searchArray[i])
{
location = i;
break;
}
location = -1;
}
if(location != -1)
{
std::cout<<searchKey<<" is at location "<<location<<" in the array.\n";
}
else
{
std::cout<<searchKey<<" is not in the array.\n";
}
}
return 0;
}
//Note-3: How to create multi-array:
/*Goal: understand multidimensional arrays in C++*/
#include<iostream>
int main()
{
int array2Dim[2][3] = {0,1,2,3,4,5};
for(int i=0; i<2;i++)
for(int j=0;j<3;j++)
std::cout<<"array2Dim["<<i<<"]["<<j<<"] = " << array2Dim[i][j]<<"\n";
return 0;
}