0% found this document useful (0 votes)
21 views

Arrays

The document provides 10 programming exercises involving arrays and basic data structures. The exercises cover topics like counting whitespace characters, calculating the norm of a vector, checking if a number is a palindrome, generating and sorting random numbers, and more.

Uploaded by

ItsSidraYo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Arrays

The document provides 10 programming exercises involving arrays and basic data structures. The exercises cover topics like counting whitespace characters, calculating the norm of a vector, checking if a number is a palindrome, generating and sorting random numbers, and more.

Uploaded by

ItsSidraYo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

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.

Sample inputs / outputs:


Enter a paragraph:
This is a test

University of Sharjah

0
There are 6 spaces, 6 tabs, and 4 new lines

#include <iostream>

using namespace std;

int main()
{
char letter;
int space = 0, tab = 0, newLine = 0;

cout << "Enter a paragraph:\n";


cin.get(letter);
while(letter != '0')
{
if(letter == ' ')
space++;
else if(letter == '\t')
tab++;
else if(letter == '\n')
newLine++;

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>

using namespace std;

const int maxSize = 30;

int main()
{
int N, i, k=0;
float A[maxSize], norm2, sum=0;

cout << "Enter how many numbers: ";


cin >> N;

cout << "Enter " << N << " numbers: ";


for(i=0; i<N; i++)
{
cin >> A[i];
sum = sum + A[i]*A[i];
}

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>

using namespace std;

const int maxSize = 30;

int main()
{
int N, i;
char A[maxSize];

cout << "Enter how many characters: ";


cin >> N;

cout << "Enter " << N << " characters: ";


for(i=0; i<N; i++)
cin >> A[i];

cout << "The upper-case letters are: ";


for(i=0; i<N; i++)
if('A' <= A[i] && A[i] <= 'Z')
cout << A[i];
cout << endl;

cout << "The lower-case letters are: ";


for(i=0; i<N; i++)
if('a' <= A[i] && A[i] <= 'z')
cout << A[i];
cout << endl;

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.

Sample inputs / outputs:


Enter a positive integer: 2378732
2378732 is a palindrome

Enter a positive integer: 2543462


2543462 is not a palindrome

#include<iostream>

using namespace std;

const int maxSize = 30;

int main()
{
int number, digits[maxSize], temp, n, i=0;

cout << "Enter a positive integer: ";


cin >> number;
temp = number;

while(temp > 0)
{
digits[i] = temp % 10;
temp = temp / 10;
i++;
}
n = i;

for(i=0; i<n/2; i++)


if(digits[i] != digits[n-i-1])
break;

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.

Sample inputs / outputs:


Enter an integer: 7654321
The digits in order: 7*6*5*4*3*2*1
The digits swapped: 6*7*4*5*2*3*1

Enter an integer: 654321


The digits in order: 6*5*4*3*2*1
The digits swapped: 5*6*3*4*1*2

const int maxSize = 50;

int main()
{
int number, i, k = 0, A[maxSize];

cout << "Enter an integer: ";


cin >> number;

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;

cout << "The digits swapped: ";


if(k%2 == 0)
{
for(i=k-1; i>1; i=i-2)
cout << A[i-1] << "*" << A[i] << "*";
cout << A[0] << "*" << A[1] << endl;
}
else
{
for(i=k-1; i>0; i=i-2)
cout << A[i-1] << "*" << 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>

using namespace std;

const int maxSize = 50;

int main()
{
int N, i, A[maxSize];

cout << "Enter an integer: ";


cin >> N;

cout << "Enter " << N << " numbers: ";


for(i=0; i<N; i++)
cin >> A[i];

cout << "The numbers are:\n";


for(i=0; i<N/2; i++)
cout << A[i] << "\t" << A[N-i-1] << endl;

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.

Sample inputs / outputs:


Enter an integer: 7
The random numbers are: 0.7649 0.9948 0.77 0.6048 0.3156 0.9808 0.9002
The random numbers are: 0.9002 0.9808 0.3156 0.6048 0.77 0.9948 0.7649

#include<iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int maxSize = 50;

int main()
{
int N, i;
float A[maxSize];

srand(time(0));

cout << "Enter an integer: ";


cin >> N;

cout << "The random numbers are: ";


for(i=0; i<N; i++)
{
A[i] = rand()%10001 / 10000.0;
cout << A[i] << " ";
}

cout << "\nThe random numbers are: ";


for(i=N-1; i>=0; i--)
cout << A[i] << " ";
cout << endl;

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.

Sample input / output:


Enter how many numbers: 7
Enter the lower and upper values: 17 239
The generated numbers are: 27 89 165 50 28 119 98
The largest number is: 165

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int arraySize = 20;

int main()
{
int N, i, maximum, lower, upper, numbers[arraySize];

srand(time(0));

cout << "Enter how many numbers: ";


cin >> N;

cout << "Enter the lower and upper values: ";


cin >> lower >> upper;

numbers[0] = lower + rand()%(upper-lower+1);


maximum = numbers[0];

cout << "The generated numbers are: ";


cout << numbers[0] << " ";
for(i=1; i<N; i++)
{
numbers[i] = lower + rand()%(upper-lower+1);
cout << numbers[i] << " ";
if(numbers[i] > maximum)
maximum = numbers[i];
}

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.

Sample input / output:


Enter how many numbers: 9

27 | ***************************
10 | **********
21 | *********************
16 | ****************
23 | ***********************
27 | ***************************
12 | ************
21 | *********************
10 | **********

#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int N, i, j, num;

cout << "Enter how many numbers: ";


cin >> N;

srand(time(0));
cout << left << endl;

for(i=0; i<N; i++)


{
num = 10 + rand()%(30-10+1);
cout << setw(5) << num << "| ";
for(j=0; j<num; j++)
cout << "*";
cout << 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.

Sample input / output:


How many decimal numbers: 7
Enter 7 decimal numbers: 2.3 5.4 7.1 12.3 8.3 17.3 26.9
The numbers do not form an increasing sequence

How many decimal numbers: 7


Enter 7 decimal numbers: 2.3 5.4 7.1 12.3 14.2 17.3 26.9
The numbers form an increasing sequence

#include <iostream>

using namespace std;

const int maxSize = 20;

int main()
{
int n, i;
double A[maxSize];

cout << "How many decimal numbers: ";


cin >> n;

cout << "Enter " << n << " decimal numbers: ";
for(i=0; i<n; i++)
cin >> A[i];

for(i=1; i<n; i++)


if(A[i] <= A[i-1])
break;

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.

Sample input / output:


How many numbers: 11
Enter 11 decimal numbers: 12 7 3 9 13 17 18 20 17 34 44
The numbers do not form a valley

How many numbers: 11


Enter 11 decimal numbers: 12 7 3 9 13 17 18 20 31 34 44
The numbers form a valley

#include <iostream>

using namespace std;

const int maxSize = 20;

int main()
{
int n, i, iLeft, iRight;
double A[maxSize];

cout << "How many numbers: ";


cin >> n;

cout << "Enter " << n << " decimal numbers: ";
for(int i=0; i<n; i++)
cin >> A[i];

for(i=1; i<n; i++)


if(A[i] <= A[i-1])
iLeft = i;

for(i=n-1; i>=1; i--)


if(A[i-1] <= A[i])
iRight = i-1;

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.

Sample input / output:


Enter the number of mines, rows, columns: 10 8 12

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>

using namespace std;

const int maxRows = 20, maxColumns = 20;

int main()
{
int numberOfMines, rows, columns, r, c;
int i, j, k, board[maxRows][maxColumns] = {0};

cout << "Enter the number of mines, rows, columns: ";


cin >> numberOfMines >> rows >> columns;

srand(time(0));

for(k=0; k<numberOfMines; k++)


{
while(true)
{
r = rand() % rows;
c = rand() % columns;
if(board[r][c] == 0)
break;
}
board[r][c] = 1;
}

cout << "\nThe board" << endl;


12
for(i=0; i<rows; i++)
{
for(j=0; j<columns; j++)
cout << board[i][j] << "\t";
cout << endl;
}

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>

using namespace std;

int main()
{
int A[10],B[10],sum=0,N;

cout << "Enter how many numbers: ";


cin >> 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];
}

cout << "The dot product is " << sum;

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>

using namespace std;

int main()
{
char t[50],U[50],L[50];
int upper=0,lower=0,N;

cout << "Enter how many characters: ";


cin >> N;

cout << "Enter " << N << " characters: ";


for(int i=0; i<N; i++)
{
cin >> t[i];

if(t[i] >= 'A' && t[i] <= 'Z')


{
U[upper] = t[i];
upper++;
}

else if(t[i] >= 'a' && t[i] <= 'z')


{
L[lower] = t[i];
lower++;
}
}

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>

using namespace std;

int main()
{
srand(time(0));

int lower,upper,x[50],N,k=0;
float sum=0,avg;

cout << "Enter an integer: ";


cin >> N;

cout << "Enter the lower and upper bounds: ";


cin >> lower >> upper;

cout << "The random numbers are:";

for(int i=0; i<N; i++)


{
if(i%7==0)
cout << endl;

x[i] = rand()%(upper-lower+1)+lower;

cout << x[i] << " ";

sum += x[i];

16
avg = sum/N;

for(int i=0; i<N; i++)


{
if(x[i] < avg)
k++;
}

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

The octal form of 172 is 254.

Sample input / output:


Enter a positive integer: 127
The octal form of 127 is 254

#include<iostream>

using namespace std;

int main()
{

int octal = 0, x, temp, remainder, n = 0;


int A[50];

cout << "Enter a number: ";


cin >> x;

temp = x;

while(temp > 0)
{
remainder = temp % 8;

17
A[n] = remainder;
n++;
temp = temp / 8;

for(int i = n - 1; i >= 0; i--)


{
octal = octal * 10 + A[i];
}
cout << "The octal form of " << x;
cout << " is " << octal;

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].

Sample input / output:


Enter the array size: 8
Enter the 8 array elements: 4 5 2 1 4 9 7 2
The mini peaks are: 5 9

#include<iostream>

using namespace std;

int main()
{
int N,A[50],B[50],s=0;

cout << "Enter the array size: ";


cin >> N;

cout << "Enter the " << N << " array elements: ";

for(int i=0; i<N; i++)


cin >> A[i];

for(int i=1; i<N-1; i++)


{
if(A[i] > A[i-1] && A[i] > A[i+1])
{
B[s] = A[i];
18
s++;
}
}

cout << "The mini peaks are: ";


for(int i=0; i<s; i++)
cout << B[i] << " ";

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.

Sample input / output:


Enter the array size: 8
Enter the 8 elements of the array: Apple Banana Apple Dog Elephant Banana Apple Grapes
The new array: Apple Banana Dog Elephant Grapes

#include<iostream>

using namespace std;

int main()
{
string A[20],B[20],c;
int N,s=0;
bool dup;

cout << "Enter the array size: ";


cin >> N;

cout << "Enter " << N << " elements of the array: ";
for(int i=0; i<N; i++)
cin >> A[i];

for(int i=0; i<N; i++)


{
dup = false;

for(int j=0; j<N; j++)


{
if(A[i] == B[j])
dup = true;
19
}

if(!dup)
{
B[s] = A[i];
s++;
}

cout << "The new array: ";


for(int i=0; i<s; i++)
cout << B[i] << " ";

return 0;
}

20

You might also like