In: Computer Science
Exercises on Arrays –using C++programming
Array |
|
|
|
|
|
|
|
|
|
|
|
Index |
|
|
|
|
|
|
|
|
|
|
|
Open up your editor and write the following program:
Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table above.
Array |
|
|
|
|
|
|
|
|
|
|
|
Index |
|
|
|
|
|
|
|
|
|
|
|
Create a program that creates this array in the main() function.
Create a program that adds the value of each previous array element to the current array element value; your program should start with a function called initArray() that initializes the array as above. Your program should output the array index, array element value, the previous array element value, and the new element value.
months |
|
|
|
|
|
|
|
|
|
|
|
numbers |
|
|
|
|
|
|
|
|
|
|
|
days |
|
|
|
|
|
|
|
|
|
|
|
index |
|
|
|
|
|
|
|
|
|
|
|
[1]---------------------------------------------
Array | 100 | 101 | 102 | 103 | 104 | 105 | |||||
Index | 0 | 1 | 2 | 3 | 4 | 5 |
#include <iostream>
using namespace std;
int main()
{
// Initializig an array
int i,a[] = {100,101,102,103,104,105};
// traversing through array
for(i=0;a[i];i++)
cout << a[i] << " ";
cout << endl << "Count : "<< i <<
endl;
return 0;
}
/* Sample output
100 101 102 103 104 105
Count : 6
*/
// Program printed, and expected are same
[2]---------------------------------------------
Array | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | ||
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
// Create a program that creates this array in the main()
function.
#include <iostream>
using namespace std;
int main()
{
int a[50] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
return 0;
}
// Create a program that adds the value of each previous array
element to the current array element value
#include <iostream>
using namespace std;
void initArray()
{
int i, prev,a[50] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
for(i=1;i<20;i++)
{
prev = a[i]; // storing existing element
a[i] = a[i] + a[i-1]; // adding previous element to it
// printing output
cout << i << "-"<< prev<< "-" <<
a[i+1]<< "-" << a[i] << endl;
}
}
int main()
{
initArray();
return 0;
}
/* Sample output
1-1-1-2
2-1-1-3
3-1-1-4
4-1-1-5
5-1-1-6
6-1-1-7
7-1-1-8
8-1-1-9
9-1-1-10
10-1-1-11
11-1-1-12
12-1-1-13
13-1-1-14
14-1-1-15
15-1-1-16
16-1-1-17
17-1-1-18
18-1-1-19
19-1-1-20
21-1-1-22
22-1-1-23
23-1-1-24
24-1-1-25
25-1-1-26
26-1-1-27
27-1-1-28
28-1-1-29
29-1-1-30
30-1-1-31
31-1-1-32
32-1-1-33
33-1-1-34
34-1-1-35
35-1-1-36
36-1-1-37
37-1-1-38
38-1-1-39
39-1-1-40
40-1-1-41
41-1-1-42
42-1-1-43
43-1-1-44
44-1-1-45
45-1-1-46
46-1-1-47
47-1-1-48
48-1-1-49
49-1-1-50
*/
[3]---------------------------------------------
Months | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | |
Numbers | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | |
Days | 31 | 28 | 31 | 30 | 31 | 30 | 31 | 31 | 30 | 31 | 30 | 31 | |
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
#include <iostream>
#include <cstring>
using namespace std;
// finds the number of days for given month
void info(int d[])
{
int i, sum = 0,x;
cout << "Enter month to find out number of days[2-Feb,7-Jul]
: ";
cin >> x;
cout << d[x-1] << endl;
}
int main()
{
// initializing arrays
string months[12] = {"Jan","Feb", "Mar", "Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"};
int a[12]={1,2,3,4,5,6,7,8,9,10,11,12};
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int sum = 0,i;
// calling to check twice
info(days);
info(days);
// looping for 12 times
for(i=0;i<12;i++)
{
sum+=days[i]; // adding the number of days
cout <<"On "<<months[i] <<" " << days[i]
<< ", there have been " << sum << " days"
<< endl;
}
return 0;
}
/* Sample run
Enter month to find out number of days[2-Feb,7-Jul] : 2
28
Enter month to find out number of days[2-Feb,7-Jul] : 7
31
On Jan 31, there have been 31 days
On Feb 28, there have been 59 days
On Mar 31, there have been 90 days
On Apr 30, there have been 120 days
On May 31, there have been 151 days
On Jun 30, there have been 181 days
On Jul 31, there have been 212 days
On Aug 31, there have been 243 days
On Sep 30, there have been 273 days
On Oct 31, there have been 304 days
On Nov 30, there have been 334 days
On Dec 31, there have been 365 days
*/