Solved

# Creating an Array

Posted on 2004-11-03
239 Views
I have this program

#include <stdio.h>

/* count digits, white space, others */
main()

{
int c, i, nwhite, nother;
int ndigit [10];

nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit [i] = 0;

while ((c = getchar ()) != EOF)
if (c >= '0' && c <= '9'')
++ndigit[c-'0'];
else if (c == ' ' || c == '\t')
++nwhite;
else
++nother;

printf ("digits = ");
for (i = 0; i < 10; ++ i )
printf(" %d", ndigit [i]);
printf(" , white space = %d, other = %d\n",
nwhite, nother);

)

it is going to count the number of occurrences of each digit, of white space characters (blank, tab newline) and of allother charcters.

It will put the out put as

digits = 9 3 0 0 0 0 0 0 1, white space = 123, other = 345

I would like the output to look like this

digits
0  9
1  3
2  0
3  0
4  0

and so on..  with the white space =123 at the bottom.  How can I change the print statement to do this?

Thanks

0
Question by:lefty431

LVL 12

Expert Comment

#include <stdio.h>
#include <stdlib.h>

/* count digits, white space, others */
main()

{
int c, i, nwhite, nother;
int ndigit [10];

nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit [i] = 0;

while ((c = getchar ()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\t')
++nwhite;
else
++nother;
printf("welcome to www.fruitfruit.com\n");
printf ("digits = ");
for (i = 0; i < 10; ++ i )
printf("%c %d\n",'0'+i, ndigit [i]);
printf(" , white space = %d, other = %d\n",
nwhite, nother);
system("PAUSE");
}
0

LVL 9

Accepted Solution

hi,

you can use printf function with \n so in output print a new line,

int i;
for(i=0; i<10; i++)
printf("%d %d\n", i, ndigit[i]);

have a good printing day;
0

LVL 1

Author Comment

let me try them.

Have a good printing day???   Not sure what that means...  but thanks..

0

LVL 2

Assisted Solution

comment given by zaghaghi will surely work.
0

LVL 4

Expert Comment

#include <stdio.h>

/* count digits, white space, others */
main() {

int c, i, nwhite, nother;
int ndigit [10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
nwhite = nother = 0;

while ((c = getchar ()) != EOF && c != '\n') {

if (c >= '0' && c <= '9')  ndigit[c-'0']++;
else if (c == ' ' || c == '\t') nwhite++;
else other++;
}

printf ("Digits:\n ");
for (i=0, i < 10; i++)  printf("%d %d\n", i, ndigit[i]);
printf("Whitespace = %d, Other = %d\n", nwhite, nother);
}

I believe that is what you're looking for.

Good luck,
Stuart Konen
0

LVL 1

Author Comment

#include <stdio.h>

/* count digits, white space, others */
main()

{
int c, i;
int nwhite, nother;
int ndigit[10];

nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;

while ((c = getchar ()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf ("digits =");
for (i = 0; i < 10; ++ i)
printf("%d %d\n", i, ndigit[i]);

printf(" , white space = %d, other = %d\n",
nwhite, nother);

}

the above code produces the following output.

digits =0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
, white space = 0, other = 0

I don't think this is right.  Can someone check this for me real quick?

0

LVL 1

Author Comment

never mind.. figured it out..  thanks all...
0

## Featured Post

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.