Using MIPS Assembly Language please The purpose of this program is to help you u
ID: 3605435 • Letter: U
Question
Using MIPS Assembly Language please
The purpose of this program is to help you understand dynamic array, system stack and multi- level subprogram calls This program will first prompt user to input a valid array size (between 4 inclusive to 11 exclusive) and then dynamically allocate an array in memory based on the size. It will then ask user to input integers one by one to fill out the allocated array until it's full. After input, the program will print out the original array and then do three operations on the array: 1· calculate the summation of all even numbers in the array and print out the result; 2. dynamically allocate a new array with the same size, reversely copy all elements from the original array in to the new array, and then print the new array to the console; 3. Prompt user for a stride n and print out every nh element in the new reversed array. To achieve this goal, you will need 1 main program associate with 7 subprograms: This subprogram will have no argument IN and two arguments OUT: $spt0 array base address (OUT) $sp+4 - array size (OUT) create array will prompt user for a size of the array and will validate the input (between 4 inclusive to 11 exclusive). If an invalid size is entered, it will print arn error message and re-prompt the user for a valid one. When a valid size is entered, createarray will call allocate array to dynamically allocate an array And then it will call read array to fill it out. It will return base address and array size to the calling location at the end of this subprogram This subprogram will have one argument IN and one arguments OUT: $spt0 - user input size (IN) $spt4 - array base address (OUT) allocate .acray will dynamically allocate enough memory to hold the array based on the input size, and then return base address of the array back to the calling location 3· read array This subprogram will have two arguments IN and no argument OUT: $spt0 array base address (IN) $sp+4 - array size (IN) read aray will read integer values from user and store them into the array until the array is full. A prompt must be printed before each entry is read (e.g. "Enter an integer: ")Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
/*
allocate_array will daynamically allocate enough memory to hold the array based on the
input size, and then return base address of the array back to the calling location.
*/
int* allocate_array(int array_size){
return new int[array_size];
}
/*
read_array will read integer values from user and store them into tlhe array until the array is full.
*/
void read_array(int array[],int array_size){
for(int i=0; i<array_size; i++){
cout<<"Enter an integer. ";
cin>>array[i];
}
}
/*
create_array will prompt user for a size of array and will validate the input
(between 4 inclusive to 11 exclusive). if an invalid size is entered, it will
print an error message and re-prompt the user for a valid one. when a valid size
is entered, create_array will call allocate_array to dynamically allocate an array.
and then it will call read_array to fill it out. it will return base address and array
size to the calling location at the end of this subprogram.
*/
pair<int*,int> create_array(){
int *array,array_size=0;
pair<int*,int>twoValues;
while(1){
cout<<"Enter size of the array ";
cin>>array_size;
if(array_size<4 || array_size>11)
cout<<"Enter valid number ";
else
break;
}
array=allocate_array(array_size);
read_array(array,array_size);
twoValues.first=array;
twoValues.second=array_size;
return twoValues;
}
/*
print_array will print all values from the array with each value separated by a space.
*/
void print_array(int array[],int array_size){
for(int i=0; i<array_size; i++){
cout<<array[i]<<" ";
}
}
/*
print_every_nth will print every nth value from the array, starting from
the first value(index 0).
*/
void print_every_nth(int array[],int array_size,int stride){
for(int i=0; i<array_size; i++){
if(i%stride==0){
cout<<array[i]<<" ";
}
}
}
/*
this subprogram will calculate the sum of all even values in ther array and return that sum to calling location.
*/
int sum_even_values(int array[],int array_size){
int sum=0;
for(int i=0; i<array_size; i++){
if(array[i]%2==0)
sum+=array[i];
}
return sum;
}
/*
this subprogram will call callocate_array to daynamically allocate a new array with the
same size as the original one, and the reversely copy all values from the original array.
*/
int* reverse_array(int array[],int array_size){
int *r_array=allocate_array(array_size);
for(int i=array_size-1,k=0; i>=0; i--,k++){
r_array[k]=array[i];
}
return r_array;
}
int main(){
int stride;
pair<int*,int>twoValues=create_array();
print_array(twoValues.first,twoValues.second);
cout<<endl<<sum_even_values(twoValues.first,twoValues.second)<<endl;
int *r_array=reverse_array(twoValues.first,twoValues.second);
cout<<"Enter Stride n ";
cin>>stride;
print_every_nth(twoValues.first,twoValues.second,stride);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.