I am having trouble making the last line of numbers in biggerArray all equal to
ID: 3761756 • Letter: I
Question
I am having trouble making the last line of numbers in biggerArray all equal to 0. I know that I need to use a for, if and else statement in the for loop, however I'm not sure how to do so. Thanks so much for the help! Below is my code:
#include <iomanip>
#include <iostream>
using namespace std;
void printArray(int[],int);
void deleteArray(int[],int, int[], int);
void biggerArray(int[],int,int[],int);
int main()
{
int intArray[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
// You do NOT need to change anything below this point of main.
/*
* This should print the following:
*
* Original array:
* 0 1 2 3 4 5 6 7 8 9
* 10 11 12 13 14 15 16 17 18 19
*/
cout<<"Original array: "<<endl;
printArray(intArray,20);
cout<<endl;
/*
* This should print the following:
*
* Smaller array:
* 0 1 2 3 4 6 7 8 9 10
* 11 12 13 14 15 16 17 18 19
*/
int smallArray[19];
cout<<"Smaller array: "<<endl;
deleteArray(intArray,20,smallArray,5);
printArray(smallArray,19);
cout<<endl;
/*
* This should print the following:
*
* Bigger array:
* 0 1 2 3 4 5 6 7 8 9
* 10 11 12 13 14 15 16 17 18 19
* 0 0 0 0 0
*/
int bigArray[19];
cout<<"Bigger array: "<<endl;
biggerArray(intArray,20,bigArray,25);
printArray(bigArray,25);
cout<<endl;
return 0;
}
/****************************************************************************
* void biggerArray(int original[], int origLen, int newArray[], int newLength)
*
* This function will use a new array of integers of length newLength
* and put all the elements of original from 0 to len in the new array and
* have the remaining elements be zero.
*
* This function assumes that newLength > len.
***************************************************************************/
void biggerArray(int original[], int origLen, int newArray[], int newLength)
{
for(int i = 0; i < origLen; i++)
{
newArray[i] = original[i];
}
}
/****************************************************************************
* void deleteArray(int original, int origLen, int newArray[], int index)
*
* This function will use a new array of length one less than the original
* array and it will remove the element at index, or rather not include
* that element in the new array.
*
* This function assumes that the index is a valid index for the given
* array.
***************************************************************************/
void deleteArray(int original[], int origLen, int newArray[], int index)
{
for(int i = 0; i < origLen; i++)
{
newArray[i] = original[i];
}
}
/***************************************************************************
* void printArray(int arr[],int len)
*
* This function will print all of th elements of the array, in lines of
* at most 10 elements, space separated.
*
* You do NOT need to change this funciton. It is correct.
**************************************************************************/
void printArray(int arr[],int len)
{
for(int i = 0; i < len; i++)
{
if (i>0 && i % 10 == 0)
cout<<endl;
cout<<setw(2)<<arr[i]<<" ";
}
cout<<endl;
}
Explanation / Answer
#include <iomanip>
#include <iostream>
using namespace std;
void printArray(int[],int);
void deleteArray(int[],int, int[], int);
void biggerArray(int[],int,int[],int);
int main()
{
int intArray[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
// You do NOT need to change anything below this point of main.
/*
* This should print the following:
*
* Original array:
* 0 1 2 3 4 5 6 7 8 9
* 10 11 12 13 14 15 16 17 18 19
*/
cout<<"Original array: "<<endl;
printArray(intArray,20);
cout<<endl;
/*
* This should print the following:
*
* Smaller array:
* 0 1 2 3 4 6 7 8 9 10
* 11 12 13 14 15 16 17 18 19
*/
int smallArray[19];
cout<<"Smaller array: "<<endl;
deleteArray(intArray,20,smallArray,5);
printArray(smallArray,19);
cout<<endl;
/*
* This should print the following:
*
* Bigger array:
* 0 1 2 3 4 5 6 7 8 9
* 10 11 12 13 14 15 16 17 18 19
* 0 0 0 0 0
*/
int bigArray[25];
for (int i=0; i<25; i++) {
bigArray[i] = 0;
}
cout<<"Bigger array: "<<endl;
biggerArray(intArray,20,bigArray,25);
printArray(bigArray,25);
cout<<endl;
return 0;
}
/****************************************************************************
* void biggerArray(int original[], int origLen, int newArray[], int newLength)
*
* This function will use a new array of integers of length newLength
* and put all the elements of original from 0 to len in the new array and
* have the remaining elements be zero.
*
* This function assumes that newLength > len.
***************************************************************************/
void biggerArray(int original[], int origLen, int newArray[], int newLength)
{
for(int i = 0; i < origLen; i++)
{
newArray[i] = original[i];
}
}
/****************************************************************************
* void deleteArray(int original, int origLen, int newArray[], int index)
*
* This function will use a new array of length one less than the original
* array and it will remove the element at index, or rather not include
* that element in the new array.
*
* This function assumes that the index is a valid index for the given
* array.
***************************************************************************/
void deleteArray(int original[], int origLen, int newArray[], int index)
{
for(int i = 0; i < origLen; i++)
{
newArray[i] = original[i];
}
}
/***************************************************************************
* void printArray(int arr[],int len)
*
* This function will print all of th elements of the array, in lines of
* at most 10 elements, space separated.
*
* You do NOT need to change this funciton. It is correct.
**************************************************************************/
void printArray(int arr[],int len)
{
for(int i = 0; i < len; i++)
{
if (i>0 && i % 10 == 0)
cout<<endl;
cout<<setw(2)<<arr[i]<<" ";
}
cout<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.