Sunday, August 2, 2009

C++ help, urgent please! Use a one dimensional array to solve the following problem. Read in 20 numbers, each?

Use a one dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it is not a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the worst case in which all 20 numbers are different. Use the smallest possible array to solve this problem.

C++ help, urgent please! Use a one dimensional array to solve the following problem. Read in 20 numbers, each?
int array[20], input, a_pos, b_pos;





a_pos = 0; // Array position


b_pos = 0;





do{


cin%26gt;%26gt; input; // Take the user input





/*if input is greater than 10 %26amp; less than 100*/


if(input!%26gt;10%26amp;%26amp;!%26lt;100){


array[a_pos] = input;


a_pos +=1;


}





}while(a_pos!=19);





/*


If you can't figure it out from here .... go away


Just put in the duplicate number checking!!!


Which will be another loop that loops n times, n being a_pos+1.


*/
Reply:It would help if you rephrased in the form of a question. What are you having difficulty with? This is not a place to have you homework done for you, but you will get help if you try doing it, get stuck and then ask about what your stuck on.
Reply:/* This is the only time I will do your homework for you. In the future, in order to get a response from me, you must first provide your own attempt at the source code and ask specific questions about where you are stuck. I wrote this entirely in the main function and did not provide any comments. OK? */








#include %26lt;iostream%26gt;


#include %26lt;conio.h%26gt;





void main()


{


int x[20];


int i, j, k, temp;


char match = 'F';





for (i=0; i%26lt;5; i++)


{


cout %26lt;%26lt; "Enter an integer between 10 %26amp; 100: ";


cin %26gt;%26gt; temp;


while (temp%26lt;10 || temp%26gt;100)


{


cout %26lt;%26lt; "Re-enter an integer between 10 %26amp; 100: ";


cin %26gt;%26gt; temp;


}





for (j=0; j%26lt;i; j++)


if (temp == x[j]) match = 'T';





if (match == 'F') x[i] = temp;


else x[i] = -1;





match = 'F';


}





cout %26lt;%26lt; '\n';


for (k=0; k%26lt;i; k++)


if (x[k] != -1) cout %26lt;%26lt; x[k] %26lt;%26lt; ' ';





getche();


}
Reply:int a[20],i,j,x,flag=0,store=0;


// to get value


for(i=0;i%26lt;20;i++)


{


cin%26gt;%26gt;x;


if(x%26gt;=10 %26amp;%26amp; x%26lt;=100)


{


for(j=0;j%26lt;=i;j++)


{ if(x==a[j])


{ flag=1;break;}


}


if(flag!=1)


{


a[i]=x;


store=i; /*contains latest postion in the array which contains some value*/


}


}


else


{ cout%26lt;%26lt;"number should b b/w 10 %26amp; 100,...enter again";


i--;


}


}


//print d values of array a which contyains only d unique values


for(i=0;%26lt;=store;i++)


cout%26lt;%26lt;a[i];


No comments:

Post a Comment