Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I cant seem to find a way to figure out this particular Q1 on this CS assigment. I coded out the print two dimensional array

I cant seem to find a way to figure out this particular Q1 on this CS assigment. I coded out the print two dimensional array void function, but I can get it it to compile correctly like what is asked on the assigment. If you need A screen shot of all the code just comment. c++ win32 console application

Q1) //Add a void function called PrintTwoDimArray, which takes as an argument the two //dimensional array called matrix and prints the values of all its elements.Print the matrix //elements in a grid(table), with all elements right justified.

============================================== // ConsoleApplication1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include using namespace std; //Function Prototypes //==================================== void PrintArray( string, int[], int); int LargestElementsIndex( int[], int); int SmallestElementIndex(int[], int); int RandomNumber(int, int); void InitializeGenerator(unsigned int); //==================================== //Constants //====================== const int MIN_RANDOM_NUMBER = 1; const int MAX_RANDOM_NUMBER = 100; const int ARRAY_SIZE = 10; //============================= //================= //THE MAIN FUNCTION //================= int main() { int n; int row; int col; int arrayIndex; int vector[ARRAY_SIZE]; //A one dimesional array. int matrix[ARRAY_SIZE][ARRAY_SIZE];// A two dimensional array. int arr[ARRAY_SIZE][ARRAY_SIZE]; InitializeGenerator(1); //============================================================== //Fill the array called vector with randomly generated values. //============================================================== n = 0; while (n < ARRAY_SIZE) { vector[n] = RandomNumber(MIN_RANDOM_NUMBER, MAX_RANDOM_NUMBER); n++; }//while PrintArray("Vector", vector, ARRAY_SIZE); arrayIndex = LargestElementsIndex(vector, ARRAY_SIZE); cout << " The index of the largest element is = " << arrayIndex << endl; cout << endl; cout << "Largest element = " << vector[arrayIndex] << endl; cout << endl; PrintArray("Vector", vector, ARRAY_SIZE); arrayIndex = SmallestElementIndex(vector, ARRAY_SIZE); cout << " The index of the smallest element is = " << arrayIndex << endl; cout << endl; cout << "Smallest element = " << vector[arrayIndex] << endl; cout << endl; // ================================================================= // Fill the Two-Dimensional array called matrix with integer values // ================================================================= row = 0; col = 0; while (row < ARRAY_SIZE) { col = 0; while (col < ARRAY_SIZE) { matrix[row][col] = row + col; col++; } row++; }//Outer while return 0; }//Function Main //=============================== // Function LargestElementsIndex //=============================== int LargestElementsIndex(int arrayData[], int numberOfElements) { int index; int maximumIndex; maximumIndex = 0; for (index = 1; index < numberOfElements; index++) { if (arrayData[index] > arrayData[maximumIndex]) maximumIndex = index; }//for return maximumIndex; }//LargestElementIndex int SmallestElementIndex(int digits[], int size) { int ind = 0; for (int i = 0; i < size; ++i) { if (digits[i] < digits[ind]) { ind = i; } } int smallest = digits[ind]; //cout << "Smallest element is = " << smallest << endl; //cout << "it is found at indices "; for (int i = 0; i < size; ++i) { if (digits[i] == smallest) { //cout << i << " "; } } cout << endl; return ind; } void PrintArray(string name, int arrayData[], int numberOfElements) { int row; row = 0; while (row < numberOfElements) { cout << name + "[" << row << "] = " << arrayData[row] << endl; row++; }//while cout << endl; }//Function PrintArray void InitializeGenerator(unsigned int seed) { srand(seed); }// Function InitializeGenerator int RandomNumber(int minNumber, int maxNumber) { return (minNumber + rand() % (maxNumber - minNumber + 1)); }//Function RandomNumber void PrintTwoDimArray(int arr[][3], int rows, int cols) { for (int i = 0;i < rows; ++i) { //irrate through each row for (int j = 0; j < cols; ++j) { // irrate through each column cout << arr[i][j] << " "; } cout << endl; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

Refer to Exercise 16.13. Are the required conditions satisfied?

Answered: 1 week ago