English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2007-01-30 15:38:56 · 2 answers · asked by Edrew c 2 in Computers & Internet Programming & Design

2 answers

Hello Edrew, here is your solution :

#include
main()
{
int a[40];
int i,r,c,n;
printf("Enter total elements to be entered (1-40) : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter element a[ %d ]=",i);
scanf("%d",&a[i]);
}
printf("\n\n");
if(n<=3)
for(i=1;i<=n;i++)
printf("%d",a[i]);
else
{
r=n % 3;
if (r != 0) {
for (i=1;i<=r;i++)
{
printf("%d",a[i]);
}
printf(",");
}
c=n/3;
for(i=1;i<=c;i++)
{printf("%d%d%d",a[r+1],a[r+2],a[r+3]);
r=r+3;
if(i!=c)
printf(",");
}
}
}

2007-01-30 15:59:04 · answer #1 · answered by Anonymous · 0 0

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 < arraySize; index++) {
if( (index > 0) && ((arraySize - index) % 3 == 0)) {
cout << ",";
}
cout << arrayOfNumbers[ index];
}

2007-01-30 16:41:03 · answer #2 · answered by Kookiemon 6 · 0 0

fedest.com, questions and answers