Question

Write a function that takes as input parameters an array of characters and a length field....

Write a function that takes as input parameters an array of characters and a length field. The function will create a square matrix from the array by first determining the largest square matrix that will hold as many numbers as possible yet not have any blank spaces. So, for instance, if the array’s length was 22, the largest square matrix would be 4x4. If the array’s length was 37, the square matrix would be 6x6.

Then create the square matrix and read the numbers from the array into the matrix (starting with arr[0] onwards).

Then print the diagonal of the matrix.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code : Whole program has been written in C++. The function required is named func.

#include<bits/stdc++.h>
using namespace std;
func(int a [], int n){
int m= sqrt(n),i,k=0,j;
int matrix[m][m];
for(i=0;i<m;i++){
for(j=0;j<m;j++)
{matrix[i][j]=a[k];
k++;}
}
cout<<"Matrix's Diagonal Elements are:\n";
for(i=0;i<m;i++)
cout<<matrix[i][i]<<" ";
}
main(){
int n,i;
cout<<"Enter size of array";
cin>>n;
int a[n];
cout<<"Enter the array numbers\n";
for(i=0;i<n;i++)
cin>>a[i];
func(a,n);
}

Explanation : The main function inputs array and passes array and its size as parameters to the function func. The function func then calculates the dimension of the matrix by taking integer part of square root of the size of array. Then the matrix is filled with array values.

Output:

Add a comment
Know the answer?
Add Answer to:
Write a function that takes as input parameters an array of characters and a length field....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MATLAB Write a function maxContent(arr) that takes a cell array arr as input and returns the...

    MATLAB Write a function maxContent(arr) that takes a cell array arr as input and returns the longest string and largest number in arr. If there are two character arrays of same length, the function should return one of them. Assume that integers in the cell array are nonnegative.

  • Write a function with two input parameters: an array of numbers and the size of the...

    Write a function with two input parameters: an array of numbers and the size of the array (the number of elements). The function should return the count of even numbers in the array. For example, if the input to the function is the array [3, 2, 45, 56, 12], the function would return the integer 3. Use the following function header: int countEvens(int nums[], int size) { }

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • a) Write a function that takes as input 2 parameters, an array and its size (both...

    a) Write a function that takes as input 2 parameters, an array and its size (both ints) and returns the max. Include the driving (main program also. To pass an array to a function, do the following: return_type my_function(int my_array[], int size) b) Write a function that does the above but can contain duplicate numbers and is capable of removing them. Scan in input. Note you may need the sizeof( ) function which is one of the standard libraries.Thus you...

  • C++ Write a function that takes as input parameters (using call by pointer) 3 integers. It...

    C++ Write a function that takes as input parameters (using call by pointer) 3 integers. It generates a random number between 25 and 50 (not including 50). It then creates an array on the memory heap of that length. It generates a random high number (mine was between 5 and 10) and a random low number (between -5 and -10) and fills in the array iteratively with random numbers between the high and the low numbers*, and it returns that...

  • MATLAB function Write an efficient function, f(v) that takes a one-dimensional array of length N^2 for...

    MATLAB function Write an efficient function, f(v) that takes a one-dimensional array of length N^2 for a square matrix (in a row-vectorized form) as an input and returns the trace of the matrix.

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • 1. The following global function takes as input parameters an array of bird objects as defined...

    1. The following global function takes as input parameters an array of bird objects as defined in Bird.h and the length of the array. It returns the bird with the largest wing span. There are multiple errors with the code. Please clearly identify them, including style issues. Bird.h class Bird { public: Bird(); ~Bird(); int WingSpan; }; Main.cpp const int MAXBIRDS 14; int main() { Bird biggest, b[MAXBIRDS]; ...some code assume is ok … biggest = LargestWingSpan(b, MAXBIRDS); return 0;...

  • Write a function that takes as an input parameter an integer that will represent the length...

    Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT