Creating an Array

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

LVL 1
lefty431Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OnegaZhangCommented:
#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
Hamed ZaghaghiProgrammerCommented:
hi,

you can use printf function with \n so in output print a new line,
for example for your code:

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

have a good printing day;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lefty431Author Commented:
let me try them.  

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

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

arun80_ininCommented:
comment given by zaghaghi will surely work.
0
SkonenCommented:
#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
lefty431Author Commented:

#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
lefty431Author Commented:
never mind.. figured it out..  thanks all...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.