Department of Mechanical Engineering
Assignment # 4
Submitted by: M. Abdul Razzaq Hussain
Sap id : 70144901
Submitted to : Sir Shahzad
Date : 20/1/2024
Question No 1#
a) What is the concept of 1- Dimensional Arrays?
A One-dimensional array is a linear collection of elements, all of the
same data type, that can be accessed using a single index or subscript.
It’s a fundamental data structure in programming, providing a way to
store and organize data sequentially in memory. Elements in a one-
dimensional array are usually accessed using their index, starting from 0
for the first element.
Define 1- Dimensional Array?
A one-dimensional array is a linear data structure consisting of a fixed-
size sequence of elements, each of the same data type, accessible by an
index or a key. Elements in the array are stored in contiguous memory
locations, and their positions are typically identified by integers starting
from 0 for the first element
b) Write the syntax of declaring 1- Dimensional Array?
The syntax for declaring a one-dimensional array varies depending on
the programming language. Here are examples in a few common
languages:
Example:
Int numbers[5];
c) Write a program in C++ that out put and input one
dimensional Arrays?
#include <iostream>
Int main() {
Const int arraySize = 5;
Int myArray[arraySize];
// Input values for the array
Std::cout << “Enter “ << arraySize << “ integers for the array:” <<
std::endl;
For (int I = 0; I < arraySize; ++i) {
Std::cout << “Element “ << I + 1 << “: “;
Std::cin >> myArray[i];
// Output the elements of the array
Std::cout << “Array elements are: “;
For (int I = 0; I < arraySize; ++i) {
Std::cout << myArray[i] << “ “;
}
Return 0;
d) Write a computer program in C++ that input a one
dimensional arrays named marks of size 5 store the obtained
Marks of five subjects . Compute and print the average marks of
subject.
#include <iostream>
Int main() {
Const int numSubjects = 5;
Int marks[numSubjects];
// Input marks for each subject
Std::cout << “Enter marks for “ << numSubjects << “ subjects:” <<
std::endl;
For (int I = 0; I < numSubjects; ++i) {
Std::cout << “Subject “ << I + 1 << “: “;
Std::cin >> marks[i];
}
// Calculate average marks
Int totalMarks = 0;
For (int I = 0; I < numSubjects; ++i) {
totalMarks += marks[i];
Double averageMarks = static_cast<double>(totalMarks) /
numSubjects;
// Print average marks
Std::cout << “Average marks: “ << averageMarks << std::endl;
Return 0;