I'm having some problems with this exercise: I have two arrays A and B. Using for loop, I need to check what elements are greater than variable x=3 and put those values into third array C. I managed to write this code:
#include <iostream>
usingnamespace std;
int main()
{
double A[100];
double B[100];
double C[200];
double n, m;
constdouble x = 3;
cout << "Type how many elements will be in array A\n";
cin >> n;
cout << "Type the elements of array A:\n";
for(int i=0; i<n; i++)
{
cin >> A[i];
}
cout << "Type how many elements will be in array B\n";
cin >> m;
cout << "Type the elements of array B:\n";
for(int i=0; i<m; i++)
{
cin >> B[i];
}
for(int i=0; i<n; i++)
{
if(A[i] > c)
{
C[i] = A[i];
cout << C[i];
}
}
for(int i=0; i<m; i++)
{
if(B[i] > c)
{
C[i] = B[i];
cout << C[i];
}
}
return 0;
}
You see, the problem is that I don't know how to make program print "The array C is empty" if there are no greater values in arrays.
(1) What if n and m are entered as greater than 100?
(2) In your if-statements, I think you meant to use 'x' and NOT 'c'
(3) You do realize that you are not actually filling up your C array normally? Depending on what numbers the user enters, some elements may have valid values and others in between may just be garbage numbers. Values may also be replaced once the B array is being checked. Make another for-loop and cout your C array and you'll see.
(4)
You see, the problem is that I don't know how to make program print "The array C is empty" if there are no greater values in arrays.
You can make else-statements to go along with each if-statement which will contain counters that increment. Then make a separate if-statement at the end asking if 'counter1' and 'counter2' are equal to n and m respectively. If yes, cout 'The Array is empty".
Although technically, arrays are never empty, but I get what you mean.
(1) The exercise tells there can't be more than 100 elements in each A and B arrays.
(2) Yeah, at first I declared that variable as c and just forgot to correct it.
(3) I understand what you mean. However I don't know how to fill array C 'correctly'.
(4) If I do the else statement like this, it prints 'The array C is empty' everytime there is at least 1 element which is not greater than x. And it's wrong, because it should only print 'The array C is empty' when all the elements are lower than x = 3.
1 2 3 4 5 6 7 8 9 10 11 12
for(int i=0; i<n; i++)
{
if(A[i] > x)
{
C[i] = A[i];
cout << C[i];
}
else
{
cout << "The array C is empty";
}
}
#include <iostream>
usingnamespace std;
int main()
{
double A[100];
double B[100];
double C[200];
double n, m;
constdouble x = 3;
cout << "Type how many elements will be in array A\n";
cin >> n;
cout << "Type the elements of array A:\n";
for(int i=0; i<n; i++)
{
cin >> A[i];
}
cout << "Type how many elements will be in array B\n";
cin >> m;
cout << "Type the elements of array B:\n";
for(int i=0; i<m; i++)
{
cin >> B[i];
}
for(int i=0; i<n; i++)
{
if(A[i] > c)
{
C[i] = A[i];
cout << C[i];
}
}
for(int i=0; i<m; i++)
{
if(B[i] > c)
{
C[i] = B[i];
cout << C[i];
}
}
return 0;
}
Ok, this program runs well with any kind of data, except if I choose A[i] and B[i] to be less than x=3. In this case, my program should print 'There are no elements from arrays A and B that are greater than x=3. Array C is empty". The problem that I don't know where and how should I write it in my program, because I have two for loops. And I cant create if statement outside the loop, because there will be an error for not understanding what is i. Hope you understand what I mean
1. declare/initialise array C
2. declare bool Empty = true
3. on the first/any 'transfer' of a number from A or B to C set Empty = false
4. finally test Empty == true or false
Now I understand. However if array A has 1 element with a value of 2 and array B has 1 element with a value of 4, it doesn't print answer well. It shows a value of 4 and it prints C is empty, which is not right, because it should only print 4.
#include <iostream>
usingnamespace std;
int main()
{
double A[100];
double B[100];
double C[200]={0};// <---
bool Empty = true;
double n, m;
constdouble c = 3;
cout << "Type how many elements will be in array A\n";
cin >> n;
cout << "Type the elements of array A:\n";
for(int i=0; i<n; i++)
{
cin >> A[i];
}
cout << "Type how many elements will be in array B\n";
cin >> m;
cout << "Type the elements of array B:\n";
for(int i=0; i<m; i++)
{
cin >> B[i];
}
for(int i=0; i<n; i++)
{
if(A[i] > c)
{
Empty = false;
C[i] = A[i];
cout << C[i];
}
}
for(int i=0; i<m; i++)
{
if(B[i] > c)
{
Empty = false;
C[i] = B[i];
cout << C[i];
}
}
if (Empty == true)
cout << "There are no elements from arrays A and B that are greater than x=3. Array C is empty";
return 0;
}
Type how many elements will be in array A
3
Type the elements of array A:
1
1
2
Type how many elements will be in array B
4
Type the elements of array B:
1
2
2
1
There are no elements from arrays A and B that are greater than x=3. Array C is empty
Exit code: 0 (normal program termination)
Now I understand. However if array A has 1 element with a value of 2 and array B has 1 element with a value of 4, it doesn't print answer well. It shows a value of 4 and it prints C is empty, which is not right, because it should only print 4.
I can't comment on your code that I cannot see. ;)
Torex wrote:
Wow, this kind of solution is interesting.
Note the solution doesn't leave you with any way to determine what values in C were placed there by your code and what values are just uninitialized garbage. An explicit flag isn't required if you keep a count of the elements added to C (which doubles as an index while you're adding to it.)
Well I don't want to steal cires thunder but what I did was implement the steps 1 to 4 I listed above. And initialising array C overcomes the count issue - if a value is zero it can't possibly be a 'transferee'.
I assume you know bool is a/the Boolean data type that takes one of only two values that equate to true or false.
So C starts off empty, i.e. the variable I named Empty is set at being true. The later two lines trap any transfer to C and so Empty is then set to false.
Since there is no statement to set Empty back to true, it remains false from then on. If it's true at the end then no transfer could have occurred so emptiness follows.