Purchase Solution

Two important exercises with Arrays

Not what you're looking for?

Ask Custom Question

Arrays are collections of elements of the same type. The elements of the array can be accessed using their position (index) in the array. Arrays may have several dimensions: they can be similar to lists (1-dimension), matrices (2-dimensions), cubes (3-dimensions) etc.
1. The first exercise involves a 1-dimensional array of integers and uses a function that returns the maximum value of the array. The program is not completely defined, the ?? should be replaced with the corresponding expressions in order to get the program work correctly.

Here is the program

#include <iostream>

using namespace std;

//declares the size of the array as a constant integer

const int SIZE = 10;

//declares the function that computes the maximum value

//the function returns an integer and has an array of integers as parameter

int max(int a[]);

int main()

{

//declares the array of integers having the size SIZE

int arrayone[SIZE];

int max_value;

//initializes each element of the array with the value of its index+1

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

arrayone[i] = i+1;

//call here the function that returns the maximum value

max_value = ??;

cout <<"The maximum element of the array is "<< max_value <<endl;

return(0);

}

int max(int a[])

{

int maximum;

//compare each element with maximum and assign its value to maximum if //greater

for (int i=??; i<??; i++)

{

if (maximum < a[i])

maximum = a[i];

}

return(maximum);

}

2. The second program declares a bi-dimensional array and uses a function to compute the sum of the elements on the main diagonal (with index [ii]). Again, the program is not completely defined, you have to replace the ?? in order to get a compilable version.

Here is the program:

#include <iostream>

using namespace std;

//declares the size of te array as a constant integer

const int SIZE = 10;

//declares the function that returns the sum of

// the elements on the main diagonal

int sum(int a[SIZE][SIZE]);

int main()

{

//declares the array of integers

int arraytwo[SIZE][SIZE];

int s;

//initializes each element of the array with the sum of its indexes

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

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

arraytwo[i][j] = i+j;

//call here the function that returns the sum of the main diagonal elements

s = ??

cout <<"The sum of the main diagonal elements is "<< s <<endl;

return(0);

}

int sum(int a[SIZE][SIZE])

{

int s=0;

//compute the sum of elements on main diagonal

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

{

s = ??

}

return(s);

Purchase this Solution

Solution Preview

These are two interesting programs to learn about arrays in C++ / C.
I have written two separate programs: 78983-1.cpp and 78983-2.cpp and in each I have extensively used comments to explain what is happening and how.

issue for 1:
// NOTE WITHOUT THE FOLLOWING LINE THE PROGRAM OUTPUT WILL BE ...

Purchase this Solution


Free BrainMass Quizzes
Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Javscript Basics

Quiz on basics of javascript programming language.

Basic Networking Questions

This quiz consists of some basic networking questions.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.