Tuesday, July 14, 2009

What is the dev c++ solution, that has an array max of 40 elements......?

What is the dev c++ solution, that has an array max of 40 elements......?


what is the dev c++ solution, that has an array max of 40 elements..the input should be from 0 to 9 only....and output the number according to its place value,, it shoud have a comma for each of the proper place value, the input should be outputed from up to down,,,,





here is the output:





Enter size: 4


4


0


9


6


Result: 4,096 (it should contain the comma (","),if it is greater than hundreds that pertains to its proper place value)





another example:


Enter size: 3


1


2


3


result: 123





another ex. hehehe:


Enter size:7


1


2


3


4


5


6


7


result: 1,234,567

What is the dev c++ solution, that has an array max of 40 elements......?
Hello Edrew, here is your solution :





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


main()


{


int a[40];


int i,r,c,n;


printf("Enter total elements to be entered (1-40) : ");


scanf("%d",%26amp;n);


for(i=1;i%26lt;=n;i++)


{


printf("Enter element a[ %d ]=",i);


scanf("%d",%26amp;a[i]);


}


printf("\n\n");


if(n%26lt;=3)


for(i=1;i%26lt;=n;i++)


printf("%d",a[i]);


else


{


r=n % 3;


if (r != 0) {


for (i=1;i%26lt;=r;i++)


{


printf("%d",a[i]);


}


printf(",");


}


c=n/3;


for(i=1;i%26lt;=c;i++)


{printf("%d%d%d",a[r+1],a[r+2],a[r+3])...


r=r+3;


if(i!=c)


printf(",");


}


}


}
Reply:Use the modulus operator(%). It takes two values and returns the remainder.





eg.


7 % 3 = 1


6 % 3 = 0


5 % 3 = 2


4 %3 = 1


3 % 3 = 0





I assume you store the inputted array size into an INT variable. Use that variable minus the array index modulus 3 to determine if it needs a comma.





eg.


for( int index = 0; index %26lt; arraySize; index++) {


if( (index %26gt; 0) %26amp;%26amp; ((arraySize - index) % 3 == 0)) {


cout %26lt;%26lt; ",";


}


cout %26lt;%26lt; arrayOfNumbers[ index];


}


No comments:

Post a Comment