Let me know my mistake in arrays program!!
-
hye!!i need to know what's wrong with this program coding..it will prompt the user to entr 10 numbers b/w 20 and 100 for this i use if structure...here if structure is used for checking whether the user enter the correct number or not. i hav lil problem that it gives the corect result if the user enter incorrect number in start but if in the middle of the program the user enter incorrect number like 12 or any incorrect no. it deals it as a correct number. plz let me know where is my mistake.one more thing i need to know plz tell me which statement i will use to check that the entered number is unique and was not previously entered by the user. Coding is below.. #include #include main() { int a[10],i; cout<<"entr 10 numbers b/w 20 and 100"<>a[i]; if((a[i]<20)||(a[i]>100)) cout<<"entr no b/w 20 and 100"<>a[i]; } getch(); } rose
the code that u have written..... only checkz for the validity of th enumber the first time(coz u have used 'if' construct! replace it by 'while'!!!! it should be.....
for(i=0;i<10;i++)
{
cin>>a[i];while((a[i]<20)||(a[i]>100))
{
cout<<"entr no b/w 20 and 100"<>a[i];
}}
to check if the number was entered or not before.....u have to check the array,then accepet or reject accordingly..... i hope thiz aint ur homework!!!!!..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
-
the code that u have written..... only checkz for the validity of th enumber the first time(coz u have used 'if' construct! replace it by 'while'!!!! it should be.....
for(i=0;i<10;i++)
{
cin>>a[i];while((a[i]<20)||(a[i]>100))
{
cout<<"entr no b/w 20 and 100"<>a[i];
}}
to check if the number was entered or not before.....u have to check the array,then accepet or reject accordingly..... i hope thiz aint ur homework!!!!!..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
How can a number be less than 20 and greater than 100 at the same time? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
-
How can a number be less than 20 and greater than 100 at the same time? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
Michael Dunn wrote: How can a number be less than 20 and greater than 100 at the same time? Only those numbers that have crossed over to the dark side. :cool:
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Michael Dunn wrote: How can a number be less than 20 and greater than 100 at the same time? Only those numbers that have crossed over to the dark side. :cool:
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
hye!!i need to know what's wrong with this program coding..it will prompt the user to entr 10 numbers b/w 20 and 100 for this i use if structure...here if structure is used for checking whether the user enter the correct number or not. i hav lil problem that it gives the corect result if the user enter incorrect number in start but if in the middle of the program the user enter incorrect number like 12 or any incorrect no. it deals it as a correct number. plz let me know where is my mistake.one more thing i need to know plz tell me which statement i will use to check that the entered number is unique and was not previously entered by the user. Coding is below.. #include #include main() { int a[10],i; cout<<"entr 10 numbers b/w 20 and 100"<>a[i]; if((a[i]<20)||(a[i]>100)) cout<<"entr no b/w 20 and 100"<>a[i]; } getch(); } rose
Your loop structure is wrong. You should use a while loop to count the number of correct numbers (unique numbers between 20 and 100). input your candidate value while the number of unique numbers between 20 and 100 is less than 10 do the following if candidate value is valid then assign it to your array and incremment your valid numbers count get the next candidate value. to check the validity of your candidate value you have to do the range check as you already figured out, and then you have to search through your array (up to the current number of valid values) for any values that are equal. If you find a match then your value is not valid. This should give you some help. But as stated, your assignments will not be done for you. Try to put this description into code. And let me know if you have further trouble. Do it quick I probably will not be online much longer. Its the weekend and I have house work to do :(
-
Your loop structure is wrong. You should use a while loop to count the number of correct numbers (unique numbers between 20 and 100). input your candidate value while the number of unique numbers between 20 and 100 is less than 10 do the following if candidate value is valid then assign it to your array and incremment your valid numbers count get the next candidate value. to check the validity of your candidate value you have to do the range check as you already figured out, and then you have to search through your array (up to the current number of valid values) for any values that are equal. If you find a match then your value is not valid. This should give you some help. But as stated, your assignments will not be done for you. Try to put this description into code. And let me know if you have further trouble. Do it quick I probably will not be online much longer. Its the weekend and I have house work to do :(
Bob Flynn wrote: Its the weekend and I have house work to do Are your weeds screaming at you too?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Bob Flynn wrote: Its the weekend and I have house work to do Are your weeds screaming at you too?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
How can a number be less than 20 and greater than 100 at the same time? --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
oopz.....i just seemed to have just noticed the "&&"..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
-
hye!!i need to know what's wrong with this program coding..it will prompt the user to entr 10 numbers b/w 20 and 100 for this i use if structure...here if structure is used for checking whether the user enter the correct number or not. i hav lil problem that it gives the corect result if the user enter incorrect number in start but if in the middle of the program the user enter incorrect number like 12 or any incorrect no. it deals it as a correct number. plz let me know where is my mistake.one more thing i need to know plz tell me which statement i will use to check that the entered number is unique and was not previously entered by the user. Coding is below.. #include #include main() { int a[10],i; cout<<"entr 10 numbers b/w 20 and 100"<>a[i]; if((a[i]<20)||(a[i]>100)) cout<<"entr no b/w 20 and 100"<>a[i]; } getch(); } rose
In Continution with Mr Rateep Code :- for checking entry of Unique Number
int nTmp,j; bool bFlag=false; int a\[10\]; for(int i=0;i<10;i++) { cin>>nTmp; while(!bFlag) { ////Here we check first Condition // no between 20 and 100 while((nTmp<20)||(nTmp>100)) { cout<<"enter no b/w 20 and 100"<\>nTmp; } j=0; while(j<=i) { if(a\[j++\]==nTmp) { bFlag=false; //again ask for unique number cout<<"You already entered this Number!enter unique no "<\>nTmp; break; } bFlag=true;
}
if(bFlag==true)
{
//save variable
a[i]=nTmp;
}}
//again make it false
bFlag=false;
}cheers, Alok Gupta
-
In Continution with Mr Rateep Code :- for checking entry of Unique Number
int nTmp,j; bool bFlag=false; int a\[10\]; for(int i=0;i<10;i++) { cin>>nTmp; while(!bFlag) { ////Here we check first Condition // no between 20 and 100 while((nTmp<20)||(nTmp>100)) { cout<<"enter no b/w 20 and 100"<\>nTmp; } j=0; while(j<=i) { if(a\[j++\]==nTmp) { bFlag=false; //again ask for unique number cout<<"You already entered this Number!enter unique no "<\>nTmp; break; } bFlag=true;
}
if(bFlag==true)
{
//save variable
a[i]=nTmp;
}}
//again make it false
bFlag=false;
}cheers, Alok Gupta
gack... that's a hell of a long code just to check for unique numbers.. if all u're interested in is getting is getting unique numbers, why not use STL to do your work for u? look at this link: http://www.cppreference.com/cppset/[^] using a set stl, it automatically keeps only the unique numbers during an insertation. and by using the pair insert( const TYPE& val ); esp, the bool, u can check if the insertation took place. for the lazy coder (that's me), my insert statement is: if (!(mySet.insert(myVal)).second) // so i don't have to use declare a pair :) cout<<"insertation has failed as it is duplicate."<
-
gack... that's a hell of a long code just to check for unique numbers.. if all u're interested in is getting is getting unique numbers, why not use STL to do your work for u? look at this link: http://www.cppreference.com/cppset/[^] using a set stl, it automatically keeps only the unique numbers during an insertation. and by using the pair insert( const TYPE& val ); esp, the bool, u can check if the insertation took place. for the lazy coder (that's me), my insert statement is: if (!(mySet.insert(myVal)).second) // so i don't have to use declare a pair :) cout<<"insertation has failed as it is duplicate."<
ng kok chuan wrote: .. if all u're interested in is getting is getting unique numbers, why not use STL to do your work for u? You are correct Buddy!,but owner of this problem seems to be beginner in C++ as you see She getting difficulty in running Simple loop so we can't expect her to program using STL. Anyways i willtry to leran from this link! i too very weak in STL :sigh: cheers, Alok Gupta