how can i check if one number is smaller than other table ?

I have to insert a number a that appears how many bread we have . after we want the diameter of the bread and the diameter for the base of the bread for each bread . i have to check for every bread if its diameter is smaller than the base diameter i write this but i don't have right answer.
can anyone help me ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;

int main (){
int a,z,p,b ;
int num;
int t;
num=2*a;
cin>>a;
for (int i = 0; i < a; i++) {
cin>>p;
}
for (int i = 0; i < a; i++) {
cin>>b;
}
if (p<=b){
    t++;
}
cout<<t<<endl;

return 0 ;
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/176481/
but i don't have right answer.


Have you compiled this yet?
- Some compilers may assume that the uninitialized variables are zero. However, it is not always the case.

What is the answer that you expect? (i.e. I type 2, but the output is 5 where it is supposed to be 7)

It would have been nice if you name the variables (i.e. base, diameter, etc), instead of letters. If this is a homework, I doubt your instructor would like the variables named as single letter. Then again, what do I know?

Some syntax errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;

int main (){
int a,z,p,b ;
int num;
int t;
num=2*a; // The variable a is not initialized.
cin>>a;
for (int i = 0; i < a; i++) {
cin>>p;
}
for (int i = 0; i < a; i++) {
cin>>b;
}
if (p<=b){
    t++; // The variable t is not initialized.
}
cout<<t<<endl;

return 0 ;
}
Last edited on
i have change the things that you say but if i insert this :
Sample Input

5
9 7 16 4 8
8 3 14 10 10
the answer must be 4 and in my programs shows 1 and i don't know the why :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;

int main (){
int a,z;
int psomi;
int basi;
int num=0;
int t=0;
num=2*a; 
cin>>a;
for (int i = 0; i < a; i++) {
cin>>psomi;
}
for (int i = 0; i < a; i++) {
cin>>basi;
}
if (psomi<=basi){
    t++; 
}
cout<<t<<endl;

return 0 ;
}
i think that the wrong is in the line 18-20 but i don't know how to solve it i think that i must write a code that check all the number (breads ) something like this is the prob
I provided the comments with the explanations on the code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;

int main(){
	int a, z;
	int psomi;
	int basi;
	int num = 0;
	int t = 0;
	num = 2 * a; // The variable a is not initialized.
	// You declare the num variable, but you do not use it anywhere in the code.
	cin >> a;

	// The for loop asks the user to input into
	//the psomi variable. However, it is saving
	// only one variable at a time. If you input
	// 9, it saves it into psomi, but once you
	//enter 7, then psomi is 7 and discards the 9.
	// if you enter 16, it discards 7 and save 16
	// into the variable and so on. For this, you 
	// want to use an array or vector to save multiple
	// numbers into the variable. For array, it should
	// look something like
	// for (int i = 0; i < a; i++)
	//     cin >> psomi[i];
	for (int i = 0; i < a; i++) {
		cin >> psomi;
	}

	// Same issue as the above for loop.
	for (int i = 0; i < a; i++) {
		cin >> basi;
	}

	// Since the variable only holds one integer,
	// then the last known value to psomi (i.e. 8)
	// will compare to the last known value to basi (i.e. 10)
	if (psomi <= basi){
		t++; // It will only increment once, because of what I explained above.
	}
	cout << t << endl; // The answer is one, because of what I explained above.

	return 0;
}


If you learned about vectors or array, then it will apply in this code.
now i change it and i write this code :
#include<iostream>
using namespace std;

int main (){
int a,z;
int psomi;
int basi;
int num=0;
int t=0;
num=2*a;
cin>>a;
for (int i = 0; i < a; i++) {
cin>>psomi;
}
for (int i = 0; i < a; i++) {
cin>>basi;
}
for (int psomi = 0; psomi < a; psomi++) {
if (psomi<=basi){
t++;
}
}
cout<<t<<endl;

return 0 ;
}
but the answer for this numbers (
5
9 7 16 4 8
8 3 14 10 10)
is 5 and it must be 4
i change my code with vector !! but how can i check all the number with the "if" in line 25-27 if my bread is smaller than a base ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<vector>
using namespace std;

int main(){
	int a, z;
	vector<int> psomi;
	vector<int> basi;
	int num = 0;
	int t = 0;

	cin >> a;

	for (int i = 0; i < a; i++) {
        cin>>a;
		psomi.push_back(i);
	}

	for (int i = 0; i < a; i++) {
    cin>>a;
		basi.push_back(i);
	}


	if (psomi <= basi){
		t++;
	}
	cout << t << endl;

	return 0;
}
chicofeo okay i will remove the one page beacause i thought that will have a more faster answer this is the reason why i make a dublicate question do you help me ?
i have to check for every bread if its diameter is smaller than the base diameter

Is there only 1 base diameter that every bread must check against it for the size? If so, to give you an idea.

1
2
3
4
5
6
for (int count = 0; count < a; count++)
      {
           if (psomi[count] <= basi)
              cout << " The Psomi # " << (count + 1) << " is smaller than Basi\n.";

      }


Now for your current code, take a look at lines 14-17. The cin >> a should be change to a variable that you could use (i.e. int size; ). Then your code to input additional elements into the vector would look like:

1
2
3
4
5
        int size = 0; // size of the bread.
	for (int i = 0; i < a; i++) {
           cin>>size;
            psomi.push_back(size);
	}
Last edited on
Topic archived. No new replies allowed.