CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2009
    Posts
    42

    Question question: creating groups of numbers and name each group and do function on groups

    hello there,..

    i am having a universal group,
    U[20] = { ... }

    Code:
    int U[20];
    for (int i=0;i<20;i++) {
    scanf("%d",&U[i]);
    }
    and inner groups which is included in the U[N].
    the user should enter the amount of groups:

    Code:
    int amount;
    scanf("%d",&amount);
    now the user must name each group with a char letter,..
    so i kinda did it as a matrix:

    first example for my work:
    if the U[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17...}
    the user enter REAL NUMBERS but the Matrix must contain the boolean value

    like for group 'a'
    the user enters:
    4,5,6,7,17

    in the first row in the matrix it must be like this:

    0 0 0 1 1 1 1 0 0 0 0 0 0 0 0,.... 1 0 0 0 0...

    the cell of common numbers will be 1 and else will be 0.


    Code:
    int list[amount][20];
    int flag=1;
    char name;
    
    for (int i=0;i<amount;i++) {
         scanf("%c",&name);
         for (int j=0;j<20;j++) {
             scanf("%d",&flag);
             if (flag == 0) { break; }
             else { 
                  for (int k=0;k<20;k++) {
                       if (U[k] == flag) {
                            list[name-'a'][j]=1;
                       }
                       else {
                            list[name-'a'][j]=0;
                       }
          }
    }
    name-'a' : refers to the row of the group .
    if name = a
    then 'a' - 'a' = 0 and it's the first group
    name = b..
    'b' - 'a' = 1 .. and it's the second group.


    the part that i need help with is the next one
    where i have to get a string and run it:

    for example if i had 3 groups a , b , c.
    the string could be a+b+c .. which is the union of all groups.

    Code:
    char str[80];
    scanf("%s",&str);
    and the question is .. how can i do a loop that knows where to stop and how to take a part the string and do it group by group..

    i need your help
    and i am willing to explain again if you didn't understand me

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: question: creating groups of numbers and name each group and do function on group

    Quote Originally Posted by sanfor View Post
    for example if i had 3 groups a , b , c.
    the string could be a+b+c .. which is the union of all groups.

    Code:
    char str[80];
    scanf("%s",&str);
    and the question is .. how can i do a loop that knows where to stop and how to take a part the string and do it group by group..
    Well in the specific example you describe, it's easy to parse the string. However, if the syntax of the user input is more complex, it may be worthwhile to build a parser for it.
    Code:
    char str[80];
    scanf("%s", &str);
    char* p = str;
    while (*p) { // while not at end of string
        char c = *p;
        if (!isalpha(c)) {
            // error
        }
        // do operator for group c
        ++p;
        if (*p) { // not at end of string
            if (*p == '+') {
                ++p;
            }
            else {
                // error
            }
        }
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured