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 ?
#include<iostream>
usingnamespace 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 ;
}
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?
#include<iostream>
usingnamespace 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 ;
}
#include<iostream>
usingnamespace 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
#include<iostream>
usingnamespace 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
#include<iostream>
#include<vector>
usingnamespace 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);
}