Menoufia University Computer Programming
Faculty of Electronic Engineering AC Year: 2023/2024
Computer Science and Eng. Program جام عة الم نوف ية Summer Semester
Sheet [3]: ARRAYS
я
1. Write a program to first input 10 values into 1-dimension array and
_ɒ
then print the sum and the average on the screen.
Ans.
єℓ
#include <iostream>
using namespace std;
int main(){
const int size = 10 ;
double numbers[size] , sum=0 ;
cout << " Enter 10 values " << endl ;
for (int i = 0 ; i < size ; i++){
cout << "number " << i+1 << " : ";
cin >> numbers[i] ;
sum += numbers[i] ;
}
cout << "- the sum = " << sum << endl;
cout << "- the average = " << sum / size << endl;
return 0;
}
2. Write a program to first input 20 values into 1-dimension array and
я
then print on the screen the sum and the average of even values in the
_ɒ
array.
Ans.
єℓ
#include <iostream>
using namespace std;
int main() {
const int size = 20 ;
int numbers[size] , sum=0 , counter =0;
cout << " Enter 20 values " << endl ;
for (int i = 0 ; i < size ; i++){
1
cout << "number " << i+1 << " : ";
cin >> numbers[i] ;
if ( numbers[i] % 2 == 0){
sum += numbers[i] ;
counter++ ;
}
я
}
_ɒ
cout << " - The sum = " << sum << endl;
cout << " - The average = " << sum / float (counter) << endl;
return 0;
єℓ
}
3. Write a program to first input 30 values into 1-dimension array and
then print on the screen the sum and the average of the values in the
odd index of the array.
Ans.
#include <iostream>
using namespace std ;
int main()
{
const int size = 5 ;
int arr[size] , sum = 0 ;
cout << " Enter 30 values for array elements : " << endl ;
for (int i = 0 ; i < size ; i++){
cin >> arr[i] ;
if (i % 2 != 0){
я
sum += arr[i] ;
}
_ɒ
cout << " - The sum = " << sum << endl;
cout << " - The average = " << sum / float (size / 2) << endl;
єℓ
return 0;
}
4. Write a C++ program to create an array representing sales of a week.
Enter the sales values of each day of the week and then calculate the
sum and the average of the sales in this week and print the results on
the screen.
2
Ans.
#include <iostream>
using namespace std ;
int main()
{
я
const int week = 7 ;
double Sales[week] , sum = 0 ;
_ɒ
cout << " Enter the sales values of each day of the week : " << endl ;
for (int i = 0 ; i < week ; i++){
єℓ
cout << " Day " << i+1 << " sales : " ;
cin >> Sales[i] ;
sum += Sales[i] ;
}
cout << " - The sum = " << sum << endl;
cout << " - The average = " << sum / week << endl;
return 0;
}
5. Write a program to allow a user to input 20 integer numbers into an
array from the keyboard, and then goes through the array elements,
looking for the largest value. Finally, print the largest number on the
screen.
Ans.
#include <iostream>
using namespace std ;
int main()
{
я
const int size = 20 ; int arr[size] ;
int largest = arr[0] ;
_ɒ
cout << " Enter 20 integer numbers : " << endl ;
for (int i = 0 ; i < size ; i++){
cout << " number " << i+1 << ": " ;
єℓ
cin >> arr[i] ;
if (arr[i] > largest ){
largest = arr[i] ;
}
}
cout << " - The largest value = " << largest << endl;
return 0;
}
3
6. Write a program to allow a user to input 30 integer numbers into an
array from the keyboard, and then goes through the array elements,
looking for the minimum value. Finally, print the minimum value on the
screen.
Ans.
я
#include <iostream>
_ɒ
using namespace std ;
int main()
{
єℓ
const int size = 30 ;
int arr[size] ;
int minval = arr[0] ;
cout << " Enter 30 integer numbers : " << endl ;
for (int i = 0 ; i < size ; i++){
cout << " number " << i+1 << ": " ;
cin >> arr[i] ;
if (arr[i] < minval ){
minval = arr[i] ;
}
}
cout << " - The minimum value = " << minval << endl;
return 0;
}
7. Write a program to find the smallest and largest values in an array of
50 integer numbers. Print the on the screen the content of the array,
the smallest and largest values.
я
Ans.
_ɒ
#include <iostream>
using namespace std ;
єℓ
int main()
{
const int size = 50 ;
int arr[size] ;
int largest = arr[0] ; int smallest = arr[0] ;
cout << " Enter 50 integer numbers : " << endl ;
for (int i = 0 ; i < size ; i++){
cout << " number " << i+1 << ": " ;
cin >> arr[i] ;
4
if (arr[i] < smallest ){
smallest = arr[i] ;
}
if (arr[i] > largest ){
largest = arr[i] ;
}
я
}
_ɒ
cout << " the content of the array : " ;
for (int i = 0 ; i < size ; i++){
cout << arr[i] << " " ;
}
єℓ
cout << "\n - The smallest value = " << smallest << endl;
cout << " - The largest value = " << largest << endl;
return 0;
}
8. Write a program to input 10 values into an array A and then compute
the values of another array B such that each element in B represent 5
times of the corresponding element in A. Finally, print both the arrays
A and B on the screen.
Ans.
#include <iostream>
using namespace std;
int main() {
const int size = 5 ;
int A[size], B[size];
cout << "Enter 10 values for array A : \n";
for (int i = 0; i < size; i++) {
я
cin >> A[i];
B[i] = A[i] * 5;
_ɒ
cout << " the array A : ";
єℓ
for (int i = 0; i < size; i++) {
cout << A[i] << " ";
}
cout << "\n the array B : ";
for (int i = 0; i < size; i++) {
cout << B[i] << " ";
}
return 0;
}
5
9. Write a program to input 30 integer numbers into 1D array and then
print on the screen the contents of the array, the sum and the average
of the values divisible by 5 in the array.
Ans.
я
#include <iostream>
_ɒ
using namespace std;
int main() {
const int size = 30 ;
єℓ
int numers[size] , sum =0 , counter =0 ;
cout << "Enter 30 values for array A : \n";
for (int i = 0; i < size; i++) {
cin >> numers[i];
if ( numers[i] %5 ==0){
sum+= numers[i] ;
counter++ ;
}
}
cout << " the contents of the array : ";
for (int i = 0; i < size; i++) {
cout << numers[i] << " ";
}
cout << "\n - the sum of the values divisible by 5 = " << sum ;
cout << "\n - the average = " << sum /float(counter) ;
return 0;
}
10. Write a program to input the sales of a company in one month in an
я
array, and then find the average of the sales in the week and in the
month as well as the sum of the sales and finally print out both the
_ɒ
average and the sum.
Ans.
єℓ
#include <iostream>
using namespace std ;
int main()
{
const int month = 30 ;
double Sales[month] , sum = 0 ;
6
cout << " Enter the sales for each day of the month : " << endl ;
for (int i = 0 ; i < month ; i++){
cout << " Day " << i+1 << " sales : " ;
cin >> Sales[i] ;
sum += Sales[i] ;
}
я
cout << " - The average of the sales in the week = " << sum / 7 << endl;
_ɒ
cout << " - The average of the sales in this month = " << sum / month << endl;
cout << " - Total sales = " << sum << endl;
return 0;
}
єℓ
11. Write a C++ program to first allow a user to input 50 integer numbers
into an array. Then, call a function to find and return the min and max
values in the array. Finally, print the result on the screen.
Ans.
#include <iostream>
using namespace std ;
void minandmax(int arr[], int size , int & minval , int& maxval ){
minval=maxval =arr[0];
for (int i = 0 ; i < size ; i++){
if (arr[i] < minval){
minval = arr[i];
}
if (arr[i] > maxval){
maxval = arr[i];
}
}
}
я
int main()
{
_ɒ
const int size = 5 ;
int numbers[size] ,minval , maxval;
cout << " Enter 50 integer numbers : " << endl ;
for (int i = 0 ; i < size ; i++){
єℓ
cin >> numbers[i] ;
}
minandmax( numbers , size , minval , maxval ) ;
cout << " - The minimum value : " << minval << endl;
cout << " - The maximum value : " << maxval << endl;
return 0;
}
7
12. Write a C++ program to first allow a user to input n integer numbers into
an array. Then, call a function to find and return the min value and call
another function to find and return the max value. Finally, print the min
and the max on the screen.
я
Ans.
_ɒ
#include <iostream>
using namespace std ;
єℓ
int min(int arr[], int size ){
int minval =arr[0];
for (int i = 0 ; i < size ; i++){
if (arr[i] < minval){
minval = arr[i];
}
}
return minval ;
}
int max(int arr[], int size ){
int maxval =arr[0];
for (int i = 0 ; i < size ; i++){
if (arr[i] > maxval){
maxval = arr[i];
}
}
return maxval ;
}
int main()
{
int n ;
cout << " Enter the number of elements : " ;
я
cin >> n ;
_ɒ
int numbers[n] ;
for (int i = 0 ; i < n ; i++){
cout << " number "<< i+1 << ": " ;
cin >> numbers[i] ;
єℓ
cout << " - The minimum value : " << min (numbers , n) << endl;
cout << " - The maximum value : " << max (numbers , n) << endl;
return 0;
}
8
13. Write a C++ program to first allow a user to input n integer
numbers into an array. Then, call a function to find and return the
sum of the array contents. Finally, print the sum and the average on
the screen.
я
Ans.
_ɒ
#include <iostream>
using namespace std ;
єℓ
int sum(int arr[], int size ){
int sum = 0;
for (int i = 0 ; i < size ; i++){
sum += arr[i];
}
return sum ;
}
int main()
{
int n ;
cout << " Enter the number of elements : " ;
cin >> n ;
int numbers[n] ;
for (int i = 0 ; i < n ; i++){
cout << " number "<< i+1 << ": " ;
cin >> numbers[i] ;
}
double totalsum = sum (numbers , n) ;
cout << " - The sum of the array contents : " << totalsum << endl;
я
cout << " - The Average : " << totalsum / n << endl;
return 0;
_ɒ
}
єℓ