Increment Array with For Loop

Hello all, I have been spending a lot of time on this site lately because I am taking a C++ class, I have found answers to many of my questions, now there are some parts of my assignment that I need help with. I am not necessarily looking for the problem to be solved, but someone to tell me what I am doing wrong.

Number 1: Copy array A into array B in reverse order.

(Array A {10 15 27 89 90 95 27 13 99 33}


Here is my attempt:

#include <iostream>
#include <fstream>

using namespace std;

void ReadFromFile(int A[]);
void Print_Array_A(int A[]);
void Copy(int A[], int B[]);
void Print_Both(int A[], int B[]);
int Find80T0100(int A[]);

int const size = 10;
int A[size], B[size];

int main()
{
ReadFromFile(A); //I got this to work
Print_Array_A(A); //I got this to work
Copy(A,B); //THIS IS NOT WORKING
Print_Both(A,B); //I got this to work
int Above80;
Above80 = Find80T0100(A); //THIS IS NOT WORKING

}

//--------------------------------------------------------------------
void Copy(int A[], int B[])

{
for (int i = 0; i < size; i++)
{
for (int j = 9; j > 0; j--)
B[j] = A[i];
}
}


//---------------------------------------------------------------------

Number 2 Question
//Call Find80To100 function to find the number of elements in the array that are between 80 and 100

My attempt
//-----------------------------------------------------------------
int Find80T0100 (int A[])

{
int result = 0;
int newarray[result];
for (int i = 0; i < size; i++)
if (A[i] > 80)
{ result = result + 1;
newarray[result] // not sure how to increment or return value
}
}


Any help is appreciated.










Your for loops that are copying the data should not be nested. The way it works currently is for each value of the array A, it assigned it to every element of B. The loop needs to look more like this:
1
2
3
for(int i = 0, j = size - 1; i < size && j > 0; ++i, --j) {
    A[i] = B[j];
}

Your counting function looks almost right. You can't return both the array and the "result" variable without something like an std::pair. Incrementing an array element is not particularly difficult; you can use any of the following:
1
2
3
newarray[result] = newarray[result] + 1;
newarray[result] += 1;
++(newarray[result]);
To return a value return value;
what is the purpose of newarray?
I have had to do a similar exercise. My question is why can't the loop be like the following?

1
2
3
for (int i = 0; i < 10; i++) {
   B[i] =  A[9-i];
}
Your loop seems perfectly OK to me. However, I would not use numbers like 10 & 9 like the ones in your loop. I would either define them as constants or deduce them from the size method to make the program work for any sizes or to easily modify them to work for any sizes.
Thank you Zhuge and who those that responded, following your correction I got the first one to work. However I am still struggling with the other question which is

Call Find80To100 function to find the number of elements in array A that are >= 80 and <=100 and print the result on the screen,

Again here is my attempt

int main()
{
ReadFromFile(A);
Print_Array_A(A);
Copy(A,B);
Print_Both(A,B);
int above80[] = Find80T0100(A);

}

//---------------------------------------------------------------------
int Find80T0100 (int A[])

{
int result = 0;
int newarray[result];

for (int i = 0; i < size; i++)
{


if (A[i] > 80)
{
newarray[result] = newarray[result] + 1;
newarray[result] += 1;
++(newarray[result]);

}
}

return newarray[result];
}


and it is not working, please help
You are returning teh wrong fing. You cannot return an array of ints in place of a single int.

Your declaration is correct. In other words, this line:
int Find80T0100 (int A[]) is fine.

Your for loop is off. How are you going to determine the size of an array?
Hint: http://cplusplus.com/forum/beginner/9291/
Oh, you have a global constant. Nevermind.

Your if checks if a value is greater than 80, not greater than 79 && less than 101- oh, I'm in a generous mood today. ;)

Finally, what's inside the if should be some increment for a single integer that you'll return at the end of the for loop.

OH! And... preferably don't store the single int in an array. This is just good form. In fact, you can just use cout << Find80T0100 (A);

-Albatross

Last edited on
Albatross,

Thanks for your feedback, actually I think that I am reading the question wrong, the question is the number of elements not the index of elements, so here is what I came up with and I hope it satisfactory for the instructor

//---------------------------------------------------------------------
int Find80T0100 (int A[])

{
int result = 0;

for (int i = 0; i < size; i++)
{


if ((A[i] >= 80) && (A[i] <= 100))
{
result += 1;
}
}

return result;
}
Topic archived. No new replies allowed.