Arrays
Arrays
Exercise #1: Whitespace A whitespace is a space , a tab \t, or a new line \n. Write a program that
reads characters and counts how many space, how many tab, and how many new line are there. Reading
stops when the zero character 0 is entered.
Hint: Use cin.get(letter) to read a character in a variable letter of type char.
University of Sharjah
0
There are 6 spaces, 6 tabs, and 4 new lines
#include <iostream>
int main()
{
char letter;
int space = 0, tab = 0, newLine = 0;
cin.get(letter);
}
cout << "There are " << space << " spaces, " << tab
<< " tabs, and " << newLine << " new lines" << endl;
return 0;
}
1
Exercise #2: Norm2 of a Vector Write a program that reads N numbers, stores them in the array A[ ], finds
the norm2 of the numbers. Norm2 of the array A is defined as the square root of the square sum of the
numbers in the array. For example, if the array A has the numbers: [2.3, 6.1, -7.9, 13.3], then norm2 of A is
the square root of (2.3)2 + (6.1)2 + (-7.9)2 + (13.3)2.
Sample inputs / outputs:
Enter how many numbers: 5
Enter 5 numbers: 3.5 8.1 21.9 16.7 11.8
Norm2 of the array is: 31.2346
#include<iostream>
#include<cmath>
int main()
{
int N, i, k=0;
float A[maxSize], norm2, sum=0;
norm2 = sqrt(sum);
cout << "Norm2 of the array is: " << norm2 << endl;
return 0;
}
2
Exercise #3: Upper-case and Lower-case Letters Write a program that reads N characters and prints only
the upper-case letters on one line and the lower-case letters on a second line.
Sample inputs / outputs:
Enter how many characters: 15
Enter 15 characters: wE are 345 Of the stuDentS
The upper-case letters are: EO
The lower-case letters are: warefthest
#include<iostream>
#include<cmath>
int main()
{
int N, i;
char A[maxSize];
return 0;
}
3
Exercise #4: Palindrome Write a program that reads a positive integer and checks whether it is a palindrome
or not. A palindrome is a number that reads the same left to right and right to left. For example, 232, 88,
43234 are palindromes.
#include<iostream>
int main()
{
int number, digits[maxSize], temp, n, i=0;
while(temp > 0)
{
digits[i] = temp % 10;
temp = temp / 10;
i++;
}
n = i;
if(i == n/2)
cout << number << " is a palindrome" << endl;
else
cout << number << " is not a palindrome" << endl;
return 0;
}
4
Exercise #5: Digits of a Number Write a program that reads an integer, prints its digits separated by stars,
prints the digits again but each two digits swapped, the second digit, then the first, the fourth then the third,
and so on.
int main()
{
int number, i, k = 0, A[maxSize];
while(number > 0)
{
A[k] = number % 10;
number = number / 10;
k++;
}
cout << "The digits in order: ";
for(i=k-1; i>0; i--)
cout << A[i] << "*";
cout << A[0] << endl;
return 0;
5
}
Exercise #6: Print the Numbers Write a program that reads N numbers and prints the first and last numbers
on one line, prints the second and one before the last numbers on the next line, and so on.
Sample inputs / outputs:
Enter an integer: 9
Enter 9 numbers: 31 12 74 10 24 65 61 87 18
The numbers are:
31 18
12 87
74 61
10 65
24
#include<iostream>
int main()
{
int N, i, A[maxSize];
if(N%2 == 1)
cout << A[N/2] << endl;
cout << endl;
return 0;
}
6
Exercise #7: Reverse the Numbers Write a program that generates N random decimal numbers each is
between 0 and 1 inclusive and each has at most four decimal digits. Print the numbers in the order they are
generated and print them again in the reverse order.
#include<iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int N, i;
float A[maxSize];
srand(time(0));
return 0;
}
7
Exercise #8: Largest Random Number Write a program that prompts the user to enter a positive integer N.
The program generates N random numbers each in the range lower to upper inclusive, and finds the largest
generated number.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int N, i, maximum, lower, upper, numbers[arraySize];
srand(time(0));
cout << "\nThe largest number is: " << maximum << endl;
return 0;
}
8
Exercise #9: Histogram of Stars Write a program that generates N number each between 10 and 30
inclusive, represents the generated numbers using a histogram of stars.
27 | ***************************
10 | **********
21 | *********************
16 | ****************
23 | ***********************
27 | ***************************
12 | ************
21 | *********************
10 | **********
#include <iomanip>
#include <cstdlib>
#include <ctime>
int main()
{
int N, i, j, num;
srand(time(0));
cout << left << endl;
return 0;
}
9
Exercise #10: Is it an Increasing Sequence Write a program that reads n decimal numbers, checks, and
prints whether the numbers form an increasing sequence or not.
#include <iostream>
int main()
{
int n, i;
double A[maxSize];
cout << "Enter " << n << " decimal numbers: ";
for(i=0; i<n; i++)
cin >> A[i];
if(i == n)
cout << "The numbers form an increasing sequence\n";
else
cout << "The numbers do not form an increasing sequence\n";
return 0;
}
10
Exercise #11: Is it a Valley Write a program that reads n numbers, checks, and prints whether the numbers
form a valley or not.
#include <iostream>
int main()
{
int n, i, iLeft, iRight;
double A[maxSize];
cout << "Enter " << n << " decimal numbers: ";
for(int i=0; i<n; i++)
cin >> A[i];
if(iLeft == iRight)
cout << "The numbers form a valley\n";
else
cout << "The numbers do not form a valley\n";
return 0;
}
11
Exercise #12: Minesweeper Write a program that generates the board of the minesweeper game. The
program prompts the user to enter the number of mines, the number of board rows and the number of board
columns. The program generates random row and column locations for the mines. In the given sample of
input / output a blank square is represented by a 0 and a mine by a 1.
The board
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 1 0 0
0 0 1 0 0 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
int numberOfMines, rows, columns, r, c;
int i, j, k, board[maxRows][maxColumns] = {0};
srand(time(0));
return 0;
}
13
Dot Product Write a program that calculate and print the dot product of two vectors each has N decimal
numbers. The dot product of two vectors is the product sum of each corresponding numbers. For example, [2,
5, 7] . [4, 8, 1] is 2*4 + 5*8 + 7*1 = 55.
Sample inputs / outputs:
Enter how many numbers: 3
Enter the 3 numbers of the first array: 2 5 7
Enter the 3 numbers of the second array: 4 8 1
The dot product is 55
#include<iostream>
int main()
{
int A[10],B[10],sum=0,N;
cout << "Enter the " << N << " numbers of the first array: ";
for(int i=0; i<N; i++)
cin >> A[i];
cout << "Enter the " << N << " numbers of the second array: ";
for(int i=0; i<N; i++)
{
cin >> B[i];
sum += A[i]*B[i];
}
return 0;
}
14
Letters in Arrays Write a program that reads N characters in the array A, save the upper-case letters in the
array U and the lower-case letters in the array L. The program then prints the two arrays U and L. See the
given sample of input / output.
Sample inputs / outputs:
Enter how many characters: 15
Enter 15 characters: wE are 345 Of the stuDentS
The upper-case letters are: EO
The lower-case letters are: warefthest
#include<iostream>
int main()
{
char t[50],U[50],L[50];
int upper=0,lower=0,N;
U[upper] = '\0';
L[lower] = '\0';
cout << "The upper-case letters are: " << U << endl;
cout << "The lower-case letters are: " << L;
return 0;
}
15
Count the Generated Numbers Write a program that generates N random numbers between lower and
upper, finds their average, and count how many generated random number is less than the average. The
program prints 7 generated numbers per line, the average with two decimal digits, and how many generated
number are less than the average.
Sample inputs / outputs:
Enter an integer: 13
Enter the lower and upper bounds: 7 23
The random numbers are
20 19 12 17 10 16 18
8 18 8 10 10 20
The average is 14.31
There are 6 numbers less than the average
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
int main()
{
srand(time(0));
int lower,upper,x[50],N,k=0;
float sum=0,avg;
x[i] = rand()%(upper-lower+1)+lower;
sum += x[i];
16
avg = sum/N;
cout << "\nThe average is " << fixed << showpoint << setprecision(2) << avg << endl;
cout << "There are " << k << " numbers less than the average";
return 0;
}
Convert to Octal Write a program that prompts the user to enter a positive integer n, convert the integer
from the decimal form to the Octal form and prints the octal number. To convert an integer to octal, divide
the integer by 3 and record the reminders until the integer is zero.
172
8 21 4
8 2 5
8 0 2
#include<iostream>
int main()
{
temp = x;
while(temp > 0)
{
remainder = temp % 8;
17
A[n] = remainder;
n++;
temp = temp / 8;
return 0;
}
Mini Peaks Write a program that reads an array of numbers, saves, and prints the mini peak numbers in
another array. A mini peak number is a number that is strictly greater than its left and right neighbors. For
example, in the array [4, 5, 2, 1, 4, 9, 7, 2] the mini peaks are [5, 9].
#include<iostream>
int main()
{
int N,A[50],B[50],s=0;
cout << "Enter the " << N << " array elements: ";
return 0;
}
Remove the Duplicate Elements of an Array Write a program that reads an array of strings, remove the
duplicate elements of the array. The program saves the unique elements of the array in a second array, then
prints the new array.
#include<iostream>
int main()
{
string A[20],B[20],c;
int N,s=0;
bool dup;
cout << "Enter " << N << " elements of the array: ";
for(int i=0; i<N; i++)
cin >> A[i];
if(!dup)
{
B[s] = A[i];
s++;
}
return 0;
}
20