Question

In: Computer Science

Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....

Exercises on Arrays –using C++programming

1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it.

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.

2. You want an array of 50 elements filled with the number 1. In the boxes below, fill in the first 8 elements of your array. Fill in the index of each element below it.

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.

3. Create an array of months, fill it with the names of the months. Create a second array and fill it with the numbers 1-12. Create a third array and fill it with the number of days in each month. Write a short program that outputs in a logical way the number of days in each month. Create a function that tells you the number of days in any month. Test with February and in July.  Total the number of days at the end of each month (e.g. your program should say: on Jan. 31, there have been 31 days. On February 28, there have been 59 days)

months

numbers

days

index

Solutions

Expert Solution

[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
*/


Related Solutions

Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Write a procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in...
Write a procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in register $a1 and Array length in register $a2 to the procedure and return Average in register $v0 to main program.
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. Your solution must include a function called isUnique() that returns 1 (true) if the number input is...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string). Write an expression that obtains the length of the third string of 's'. You may assume that string.h has been included. 2. Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3: #include <string.h> #include <stdlib.h> char**...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT