• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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

0
lefty431
Asked:
lefty431
2 Solutions
 
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 ZaghaghiCommented:
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
 
lefty431Author Commented:
let me try them.  

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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now