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

Assignment Three

This document contains the solutions to 5 coding assignments involving arrays in C++. The assignments cover displaying array elements, displaying in reverse order, taking user input to populate an array, converting days to years/weeks/days, and accessing a specific element by index. For each assignment, the code provided demonstrates how to complete the task, and sample output is shown. The document includes the student's identifying information and is numbered from pages 1-6.

Uploaded by

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

Assignment Three

This document contains the solutions to 5 coding assignments involving arrays in C++. The assignments cover displaying array elements, displaying in reverse order, taking user input to populate an array, converting days to years/weeks/days, and accessing a specific element by index. For each assignment, the code provided demonstrates how to complete the task, and sample output is shown. The document includes the student's identifying information and is numbered from pages 1-6.

Uploaded by

Mohamed Abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment Three

Name : Mohamed Abdullah Mohamed Ali

ID : 20221393

Group : A1.2

T.A : Eng. Dina ayman


Assignment Three
1. Displaying an array elements

ANSWER:
#include <iostream>
using namespace std;
int main() {
int arr[] = {60, 20, 80, 94, 54};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "the array elements is : "<<endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " " ;
}
cout << endl;
return 0;
}

Output 1:

Mohamed abdullah Page 1 of 6


Assignment Three
2. Displaying array elements in reverse order

Answer:
#include <iostream>
using namespace std;
int main() {
int arr[] = {60, 20, 80, 94, 54};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "the array elements in reverse order is : "<<endl;
for (int i = size - 1; i >= 0; i--)
{
cout << arr[i] << " " ;
}
cout << endl;
return 0;
}

Output 1 :

Mohamed abdullah Page 2 of 6


Assignment Three
3. Take inputs from user and store them in an array then display them

Answer:
#include <iostream>
using namespace std;
int main() {
int arr[100000];
int n;
cout << "HI! USER, WELCOME TO OUR PROGRAM:)"<<endl;
cout << "How many number of elements do you want? "<<endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Please,Enter element " << i + 1 << ": " ;
cin >> arr[i];
}
cout << "The elements of the array are: ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

Mohamed abdullah Page 3 of 6


Assignment Three
Output 1:

Mohamed abdullah Page 4 of 6


Assignment Three
4. Write a program to read days and convert it to years, weeks, and days.

Answer:
#include <iostream>
using namespace std;
int main() {
int arr[] = {100, 96, 75, 66, 69};
int size = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
cout << "the array elements is : "<<endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " " ;
}
cout << endl;
for (int i = 0; i < size; i++)
{
sum += arr[i];
}
cout << "The sum of the elements is: " << sum << endl;

return 0;
}

Output 1:

Mohamed abdullah Page 5 of 6


Assignment Three
5. Create an array with size 5 and display element of index 3

Answer:
#include <iostream>
using namespace std;
int main() {
int arr[5] = {88, 96, 52, 44, 69};
int index = 3;
cout << "The element at index " << index << " is: " << arr[index] << endl;
return 0;
}

OUTPUT 1:

Mohamed abdullah Page 6 of 6

You might also like