This lab has several parts, each worth an equal amount of credit. Please put all
ID: 652241 • Letter: T
Question
This lab has several parts, each worth an equal amount of credit. Please put all three parts into one file. Title it ArrayLab. ,St u de n IN a me. and comment the code appropriately to distinguish different parts of the lab: Create a function call sumArray which will return the sum of an array that is passed in. Note that a length of the array should also be specified. input: {1, 2, 3,4), length = 3 returns: 6 (because the 4 was ignored because it was past the length)Create a function call coive'tToDec mal. Th s method will take an intarray{3, 2, 4} and a length of the array, and will return a single integer where each part of the array is a digit in the new integer (324). To do this you will need to multiply: For 5% extra credit add an optional parameter that allows it to take a base to multiply it to such as base 2 or base 8 (in the example I'm using base 10 of course). Write a function pcmtAccendmg, that will take an ioJ array and the length as its input and output each integer on a line that is greater than the last. If one is smaller it will drop to a new line and continue from there: input {2, 5, 8, 3,9, 9, 7) output: 2.5.8 3.9.9 7 Write a function reverseArrayEven which will loop through a given array (with a size provided as well), and will reverse the digits if they are even numbers. input {1, 2, 4, 5, 6, 9, 10} returns: {10,9. 4, 5, 6. 2,1} Write a function to search a 2-dimentional array for a value. The method should take a reference to a row and col integer, and if the value is found then set the row and col variables accordingly. If the value is not found row and col should both equal -1. Note that lengths of the array should also be taken as input.Explanation / Answer
1)
Method :sumArray
//The method sumArray that takes the integer
//array and integer as arguments and returns
//the sum of the array elements of given size
int sumArray(int arr[],int LENGTH)
{
//index variable
int index;
//total sum
int total=0;
//add sum of value of LENGTH
for(index=0;index<LENGTH;index++)
total+=arr[index];
//return total sum;
return total;
}
Test program
#include<iostream>
using namespace std;
int sumArray(int a[],int LENGTH);
int main()
{
int arr[]={1,2,3,4};
const int LENGTH=3;
cout<<"Sum : "<<sumArray(arr,LENGTH)<<endl;
system("pause");
return 0;
}
//The method sumArray that takes the integer
//array and integer as arguments and returns
//the sum of the array elements of given size
int sumArray(int arr[],int LENGTH)
{
//index variable
int index;
//total sum
int total=0;
//add sum of value of LENGTH
for(index=0;index<LENGTH;index++)
total+=arr[index];
//return total sum;
return total;
}
Sample output:
Sum:6
-----------------------------------------------
2)
//Method type-1: convertToDecimal
//The method sumArray that takes the integer
//array and integer as arguments and returns
//the sum of the array elements of given size
double convertToDecimal(int arr[],int LENGTH)
{
//index variable
int index;
//total sum
double digit=0;
int power=0;
//add sum of value of LENGTH
for(index=LENGTH-1;index>=0;index--)
digit+=arr[index]*pow(10.0,power++);
//return total sum;
return digit;
}
//Method type-2
////Method type-2: convertToDecimal with base
//The method sumArray that takes the integer
//array ,length and base as arguments and returns
//the sum of the array elements of given size
double convertToDecimal(int arr[],int LENGTH,int base)
{
//index variable
int index;
//total sum
double digit=0;
int power=0;
//add sum of value of LENGTH
for(index=LENGTH-1;index>=0;index--)
digit+=arr[index]*pow((double)base,power++);
//return total sum;
return digit;
}
Test program
#include<iostream>
#include<math.h>
using namespace std;
//function prototype without base ,default base is 10
double convertToDecimal(int a[],int LENGTH);
//function prototype with base
double convertToDecimal(int a[],int LENGTH,int base);
int main()
{
//array of integer type
int arr[]={3,2,4};
//set lenght
const int LENGTH=3;
//for base 10
cout<<"base-10"<<endl;
cout<<"Digit : "<<convertToDecimal(arr,LENGTH)<<endl;
//for base 2
cout<<"base-2"<<endl;
cout<<"Digit : "<<convertToDecimal(arr,LENGTH,2)<<endl;
system("pause");
return 0;
}
//Method type-1
//The method sumArray that takes the integer
//array and integer as arguments and returns
//the sum of the array elements of given size
double convertToDecimal(int arr[],int LENGTH)
{
//index variable
int index;
//total sum
double digit=0;
int power=0;
//add sum of value of LENGTH
for(index=LENGTH-1;index>=0;index--)
digit+=arr[index]*pow(10.0,power++);
//return total sum;
return digit;
}
//Method type-2
//The method sumArray that takes the integer
//array ,length and base as arguments and returns
//the sum of the array elements of given size
double convertToDecimal(int arr[],int LENGTH,int base)
{
//index variable
int index;
//total sum
double digit=0;
int power=0;
//add sum of value of LENGTH
for(index=LENGTH-1;index>=0;index--)
digit+=arr[index]*pow((double)base,power++);
//return total sum;
return digit;
}
sample output:
base-10
Digit : 324
base-2
Digit : 20
-----------------------------------------------
-----------------------------------------------
3)
Method :printAscending
//The method that prints in order
void printAscending(int arr[],int LENGTH)
{
//index variable
int index1;
int index2;
//total sum
double digit=0;
int count=0;
//add sum of value of LENGTH
for(index1=0;index1<LENGTH;index1++)
{
cout<<arr[index1];
count++;
if(count==3)
{
cout<<endl;
count=0;
}
}
}
-----------------------------------------------
//Test program
#include<iostream>
#include<math.h>
using namespace std;
//function prototype
void printAscending(int a[],int LENGTH);
int main()
{
//array of integer type
int arr[]={2,5,8,3,9,9,7};
//set lenght
const int LENGTH=7;
printAscending(arr,LENGTH);
cout<<endl;
system("pause");
return 0;
}
//The method that prints in order
void printAscending(int arr[],int LENGTH)
{
//index variable
int index1;
int index2;
//total sum
double digit=0;
int count=0;
//add sum of value of LENGTH
for(index1=0;index1<LENGTH;index1++)
{
cout<<arr[index1];
count++;
if(count==3)
{
cout<<endl;
count=0;
}
}
}
----------------------------------------------------------
-----------------------------------------------
4)
//method: reverseArrayEven(int arr[],int LENGTH)
//The method that prints reverse if they are even numbers
//Condition two values must not be even or atleast one is odd
//then swap the numbers
//and returns the pointer address of the array ,arr
int* reverseArrayEven(int arr[],int LENGTH)
{
//index variable
int index;
int last=LENGTH-1;
//add sum of value of LENGTH
for(index=0;index<LENGTH/2;index++,last--)
{
if((arr[index]%2 !=0 && arr[last]%2!=0) ||arr[index]%2 ==1 || arr[last]%2==1)
{
int temp=arr[last];
arr[last]=arr[index];
arr[index]=temp;
}
}
return arr;
}
-----------------------------------------------------
Test program
#include<iostream>
#include<math.h>
using namespace std;
//function prototype
int* reverseArrayEven(int a[],int LENGTH);
void print(int a[],int LENGTH);
int main()
{
//array of integer type
int arr[]={1,2,4,5,6,9,10};
//set lenght
const int LENGTH=7;
//calling method reverseArrayEven
cout<<"input"<<endl;
print(arr,LENGTH);
//declare a pointer that takes the starting address of the array
int *pointerArr=reverseArrayEven(arr,LENGTH);
//print elements
cout<<" output"<<endl;
print(arr,LENGTH);
cout<<endl;
system("pause");
return 0;
}
//The method that prints reverse if they are even numbers
//Condition two values must not be even or atleast one is odd
//then swap the numbers
//and returns the pointer address of the array ,arr
int* reverseArrayEven(int arr[],int LENGTH)
{
//index variable
int index;
int last=LENGTH-1;
//add sum of value of LENGTH
for(index=0;index<LENGTH/2;index++,last--)
{
if((arr[index]%2 !=0 && arr[last]%2!=0) ||arr[index]%2 ==1 || arr[last]%2==1)
{
int temp=arr[last];
arr[last]=arr[index];
arr[index]=temp;
}
}
return arr;
}
//method to print the array
void print(int arr[],int LENGTH)
{
int index;
for(index=0;index<LENGTH;index++)
cout<<arr[index]<<" ";
}
sample output:
input
1 2 4 5 6 9 10
output
10 9 4 5 6 2 1
--------------------------------------------------------
-----------------------------------------------
5)
//method
//The method search takes the input argumnets row size, column size,two reference variables
//foundRow and foundCol ,search value and two dimensional array
void search(int ROW_SIZE,int COLUMN_SIZE,int &foundRow,int &foundCol,int searchValue,int arr[][3])
{
//index variable
int row;
int col;
//Loop through the values in the array
for(row=0;row<ROW_SIZE;row++)
{
for(col=0;col<COLUMN_SIZE;col++)
if(arr[row][col]==searchValue)
{
//set row and column to foundRow and foundCol
foundRow=row;
foundCol=col;
}
}
}
--------------------------------------
//Test Program
#include<iostream>
#include<math.h>
using namespace std;
//function prototype
void search(int ROW_SIZE,int COLUMN_SIZE,int &foundRow,int &foundCol,int searchValue,int arr[][3]);
int main()
{
//constant size for row and column size of two dimensional array
const int ROW_SIZE=3;
const int COL_SIZE=3;
int foundRow=-1;
int foundCol=-1;
//two dimensional array with elements are initialized in each row for three
//rows
int arr[ROW_SIZE][COL_SIZE]={{1,2,3},
{4,5,6},
{7.8,9}};
//search element
int searchValue=6;
//calling search method with row size,column size, foundRow ,foundCol ,search value and array
search(ROW_SIZE,COL_SIZE,foundRow,foundCol,searchValue,arr);
if(foundRow!=-1 && foundCol!=-1)
cout<<"search element "<<searchValue<<" found at "<<foundRow<<" and "<<foundCol<<
" index positions ."<<endl;
else
cout<<"Search element not found ."<<endl;
system("pause");
return 0;
}
//The method search takes the input argumnets row size, column size,two reference variables
//foundRow and foundCol ,search value and two dimensional array
void search(int ROW_SIZE,int COLUMN_SIZE,int &foundRow,int &foundCol,int searchValue,int arr[][3])
{
//index variable
int row;
int col;
//Loop through the values in the array
for(row=0;row<ROW_SIZE;row++)
{
for(col=0;col<COLUMN_SIZE;col++)
if(arr[row][col]==searchValue)
{
//set row and column to foundRow and foundCol
foundRow=row;
foundCol=col;
}
}
}
//sample output:
search element 6 found at 1 and 2 index positions .
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.