I have a question regarding arrays from my intro class, how would I print the fo
ID: 674422 • Letter: I
Question
I have a question regarding arrays from my intro class, how would I print the following in C++ without using system("PAUSE") or cin.ignore?
part c)
/**
* Requires: size > 0 and size is the number of elements in array
* Modifies: array
* Effects : Negates each element in array.
*/
void negate_all(int array[], int size);
part d)
/**
* Requires: size > 0 and size is the number of elements in source
* and target
* Modifies: target
* Effects : Copies the positive elements from source into adjacent
* locations in target, starting at index 0. Any remaining
* indices in target are left unchanged.
*/
void copy_positive_elements(const int source[], int target[], int size);
Thanks for the help!
Explanation / Answer
// part c)
void negate_all(int array[], int size)
{
int i;
for(i=0;i<size;i++)
{
int temp= -array[i]; // temp will store the negative of array element at index i and then assign temp to array element in next step.
array[i]=temp;
}
}
// part D)
void copy_positive_elements(const int source[], int target[], int size);
{
int i,j=0;
for(i=0;i<size;i++)
{
if(source[i]>0)
{
target[j]=source[i]; // you can not use variable i in both arrays because you have to store in adjacent locations in array target.
j++; // After storing everytime you need to increase the j to store in next adjacent position.
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.