Thread: Hello guys, I have a question regarding Arrays

  1. #46
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by SirPrattlepod View Post
    Yeah. Sorry

    You're getting closer anyway

    Code:
    found = 0;
    for (k = 0; k < ????; k++)    /* for every element of intersection[] */
    {
        if (A[i] == intersection[k])
            found = 1;                /* A[i] is already in the intersection array */
    }
    
    if (found == 0)     /* if not already in intersection */
    {
       ??????
    }
    Replace the ?????s with the correct code.

    Note that there is an opportunity to exit the for loop early (as soon as a match is found) but I don't know if you've learned about the && operator or the break keyword yet.
    Code:
    found = 0;    for (k = 0; k < SIZE; k++)    /* for every element of intersection[] */
        {
            if (A[i] == intersection[k])
                found = 1;                /* A[i] is already in the intersection array */
        }
    
    
        if (found == 0)     /* if not already in intersection */
        {
            found=intersection[]+A[i];
            counter++;
        }

  2. #47
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    In addendum to my previous post, it seems you're now writing almost random code hoping that *something* works instead of thinking about the problem and what your program is doing. I'm guessing that this is because you're getting stressed because of the deadline. Learn to deal with stress more constructively. Don't take offence at this observation it's something that's probably human nature and dealing with it effectively is a learned skill that takes practice.

  3. #48
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Code:
    #include <stdio.h>
    
    #define SIZE 10
    #define SIZE1 12
    
    
    int main(void)
    {
        int i;
        int j;
        int A[SIZE];
        int B[SIZE1];
        int intersection[SIZE];
        int counter = 0;
        int found;
        int k;
    
    
        for (i = 0; i < SIZE; i++)
        {
            printf("Enter number: ");
            scanf("%d", &A[i]);
        }
        for(i = 0; i < SIZE; i++)
        {
            printf("%d\n", A[i]);
        }
        for (i = 0; i < SIZE1; i++)
        {
            printf("Enter number: ");
            scanf("%d", &B[i]);
        }
        for(i = 0; i < SIZE1; i++)
        {
            printf("%d\n", B[i]);
        }
    
    
        for(i = 0; i < SIZE; i++)
        {
            for(j = 0; j < SIZE1; j++)
            {
                if (A[i] == B[j])
                {
                    intersection[counter] = A[i];
                    counter++;
                }
            }
        }
    
    
        found = 0;
        for (k = 0; k < SIZE; k++)    /* for every element of intersection[] */
        {
            if (A[i] == intersection[k])
                found = 1;                /* A[i] is already in the intersection array */
        }
    
    
        if (found == 0)     /* if not already in intersection */
        {
            found=intersection+A[i];
            counter++;
        }
    
    
        if (counter > 1)
        {
            printf("The intersection is: ");
            for(j = 0; j < counter; j++)
                printf("%d ", intersection[j]);
            printf("\n");
        }
        else
            printf("There is no intersection\n");
    
    
        return 0;
    }
    Hello guys, I have a question regarding Arrays-aaaaaaaa-png

    Still does the same, must have messed up your code somewhere.

  4. #49
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by SirPrattlepod View Post
    In addendum to my previous post, it seems you're now writing almost random code hoping that *something* works instead of thinking about the problem and what your program is doing. I'm guessing that this is because you're getting stressed because of the deadline. Learn to deal with stress more constructively. Don't take offence at this observation it's something that's probably human nature and dealing with it effectively is a learned skill that takes practice.
    Honestly this is what is happening. I'll read up on your pseudocode before keeping posting nonsense. My bad pal.

  5. #50
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you need to print all the intersections, then you can use logic like this:

    Code:
    #include <stdio.h>
    
    #define MAX7 7
    #define MAX10 10
    
    int main(void) {
    
       int i,j;
       int grid7[MAX7];
       int grid10[MAX10];
    
       for(i=0;i<MAX7;i++)
          grid7[i]=i;
    
       for(i=0;i<MAX10;i++)
          grid10[i]=i;
    
       for(i=0;i<MAX10;i++) {
          for(j=i;j<MAX7;j++) {  //this is key: j=i.
             if(grid10[i]==grid7[j])
                printf("%d \n",i);
          }
       }
    
       return 0;
    }
    Output:
    0123456

    Just one print out of each intersection
    Last edited by Adak; 10-04-2013 at 11:33 PM.

  6. #51
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Cdd101 View Post
    ...
    Still does the same, must have messed up your code somewhere.
    Well that's almost it.

    Code:
        found = 0;
        for (k = 0; k < SIZE; k++)    /* for every element of intersection[] */
        {
            if (A[i] == intersection[k])
                found = 1;                /* A[i] is already in the intersection array */
        }
    
    
        if (found == 0)     /* if not already in intersection */
        {
               intersection[counter] = A[i];                               // <----- FIXED THIS LINE
            counter++;
        }
    The only other problem is you've put it in the wrong place. The snippet above should replace lines 45 and 46
    Last edited by SirPrattlepod; 10-04-2013 at 11:46 PM.

  7. #52
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by SirPrattlepod View Post
    Well that's almost it.

    Code:
        found = 0;
        for (k = 0; k < SIZE; k++)    /* for every element of intersection[] */
        {
            if (A[i] == intersection[k])
                found = 1;                /* A[i] is already in the intersection array */
        }
    
    
        if (found == 0)     /* if not already in intersection */
        {
               intersection[counter] = A[i];                               // <----- FIXED THIS LINE
            counter++;
        }
    The only other problem is you've put it in the wrong place. The snippet above should replace lines 45 and 46
    Thank you so much pal
    Code:
    #include <stdio.h>
    
    #define SIZE 10
    #define SIZE1 12
    
    
    int main(void)
    {
        int i;
        int j;
        int A[SIZE];
        int B[SIZE1];
        int intersection[SIZE];
        int counter = 0;
        int found;
        int k;
    
    
        for (i = 0; i < SIZE; i++)
        {
            printf("Enter number: ");
            scanf("%d", &A[i]);
        }
        for(i = 0; i < SIZE; i++)
        {
            printf("%d\n", A[i]);
        }
        for (i = 0; i < SIZE1; i++)
        {
            printf("Enter number: ");
            scanf("%d", &B[i]);
        }
        for(i = 0; i < SIZE1; i++)
        {
            printf("%d\n", B[i]);
        }
    
    
        for(i = 0; i < SIZE; i++)
        {
            for(j = 0; j < SIZE1; j++)
            {
                if (A[i] == B[j])
                {
                    intersection[counter] = A[i];
                    counter++;
                }
            }
        }
    
    
        found = 0;
        for (k = 0; k < SIZE; k++)
        {
            if (A[i] == intersection[k])
                found = 1;
        }
    
    
        if (found == 0)
        {
            intersection[counter] = A[i];
            counter++;
        }
    
    
        if (counter > 1)
        {
            printf("The intersection is: ");
            for(j = 0; j < counter; j++)
                printf("%d ", intersection[j]);
            printf("\n");
        }
        else
            printf("There is no intersection\n");
    
    
        return 0;
    }
    This is the code and this is what it outputted this is fine. I'm tired of this assignment

    Hello guys, I have a question regarding Arrays-capture-png

  8. #53
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Well, you're too close to get tired of it

    Code:
    #include <stdio.h>
     
    #define SIZE 10
    #define SIZE1 12
     
     
    int main(void)
    {
        int i;
        int j;
        int A[SIZE];
        int B[SIZE1];
        int intersection[SIZE];
        int counter = 0;
        int found;
        int k;
     
     
        for (i = 0; i < SIZE; i++)
        {
            printf("Enter number: ");
            scanf("%d", &A[i]);
        }
        for(i = 0; i < SIZE; i++)
        {
            printf("%d\n", A[i]);
        }
        for (i = 0; i < SIZE1; i++)
        {
            printf("Enter number: ");
            scanf("%d", &B[i]);
        }
        for(i = 0; i < SIZE1; i++)
        {
            printf("%d\n", B[i]);
        }
     
     
        for(i = 0; i < SIZE; i++)
        {
            for(j = 0; j < SIZE1; j++)
            {
                if (A[i] == B[j])
                {
                    found = 0;
                    for (k = 0; k < counter; k++)
                    {
                        if (A[i] == intersection[k])
                            found = 1;
                    }
                    if (found == 0)
                    {
                        intersection[counter] = A[i];
                        counter++;
                    }
                }
            }
        }
     
        if (counter > 1)
        {
            printf("The intersection is: ");
            for(j = 0; j < counter; j++)
                printf("%d ", intersection[j]);
            printf("\n");
        }
        else
            printf("There is no intersection\n");
     
     
        return 0;
    }

  9. #54
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by SirPrattlepod View Post
    Well, you're too close to get tired of it

    Code:
    #include <stdio.h>
     
    #define SIZE 10
    #define SIZE1 12
     
     
    int main(void)
    {
        int i;
        int j;
        int A[SIZE];
        int B[SIZE1];
        int intersection[SIZE];
        int counter = 0;
        int found;
        int k;
     
     
        for (i = 0; i < SIZE; i++)
        {
            printf("Enter number: ");
            scanf("%d", &A[i]);
        }
        for(i = 0; i < SIZE; i++)
        {
            printf("%d\n", A[i]);
        }
        for (i = 0; i < SIZE1; i++)
        {
            printf("Enter number: ");
            scanf("%d", &B[i]);
        }
        for(i = 0; i < SIZE1; i++)
        {
            printf("%d\n", B[i]);
        }
     
     
        for(i = 0; i < SIZE; i++)
        {
            for(j = 0; j < SIZE1; j++)
            {
                if (A[i] == B[j])
                {
                    found = 0;
                    for (k = 0; k < counter; k++)
                    {
                        if (A[i] == intersection[k])
                            found = 1;
                    }
                    if (found == 0)
                    {
                        intersection[counter] = A[i];
                        counter++;
                    }
                }
            }
        }
     
        if (counter > 1)
        {
            printf("The intersection is: ");
            for(j = 0; j < counter; j++)
                printf("%d ", intersection[j]);
            printf("\n");
        }
        else
            printf("There is no intersection\n");
     
     
        return 0;
    }
    Thank you so much! From what I see, the only difference from my code to yours was that you
    united this

    Code:
     for(i = 0; i < SIZE; i++)    {
            for(j = 0; j < SIZE1; j++)
            {
                if (A[i] == B[j])
                {
                    found = 0;
                    for (k = 0; k < counter; k++)
                    {
                        if (A[i] == intersection[k])
                            found = 1;
                    }
                    if (found == 0)
                    {
                        intersection[counter] = A[i];
                        counter++;
                    }
                }
            }
        }

  10. #55
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Cdd101 View Post
    Thank you so much! From what I see, the only difference from my code to yours was that you
    united this

    Code:
     for(i = 0; i < SIZE; i++)    {
            for(j = 0; j < SIZE1; j++)
            {
                if (A[i] == B[j])
                {
                    found = 0;
                    for (k = 0; k < counter; k++)
                    {
                        if (A[i] == intersection[k])
                            found = 1;
                    }
                    if (found == 0)
                    {
                        intersection[counter] = A[i];
                        counter++;
                    }
                }
            }
        }
    Correct. I also fixed k < counter as the condition for the for loop.

    You could exit the loop early (no need to keep checking if found is already 1) by changing the for loop to

    Code:
    for (k = 0; k < counter && found == 0; k++)
    But only if you've learned about the && operator

    After you've submitted your assignment please summarise what you've learnt.

    The found variable, by the way, is referred to as a flag.

  11. #56
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by SirPrattlepod View Post
    Correct. I also fixed k < counter as the condition for the for loop.

    You could exit the loop early (no need to keep checking if found is already 1) by changing the for loop to

    Code:
    for (k = 0; k < counter && found == 0; k++)
    But only if you've learned about the && operator

    After you've submitted your assignment please summarise what you've learnt.

    The found variable, by the way, is referred to as a flag.
    What I learned from this assignment and it's something that has I've been wondering for a while is that you can do use a for, inside a for loop and an if as well. I always wondered about this since my teachers notes never had this on there. I'm extremely thankful for your help pal. Teachers notes have been to vague. His example programs, and definition don't really help when it comes to making each assignment.

  12. #57
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    You've also learned that stress can make your mind dysfunctional and what a flag is Stay calm.

    I could have given you the code 9 hours ago but, well, what would you have learned then?

    Edit: Oh, by the way, next weekend maybe try and write the program again. There are other ways to do it... see if you can make a better program. Hopefully you learn about functions soon and the code will be more readable as well. Good luck
    Last edited by SirPrattlepod; 10-05-2013 at 12:09 AM.

  13. #58
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by SirPrattlepod View Post
    You've also learned that stress can make your mind dysfunctional and what a flag is Stay calm.

    I could have given you the code 9 hours ago but, well, what would you have learned then?
    Oh I was stressed, I was also doing a Java homework assignment earlier . Surprisingly find my Java class much more easier :|. Thank you, I'm very grateful for your help, I learned a lot from your help.

    Since I really seem to struggle with my teachers notes would you recommend a book or something to learn from?

  14. #59
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Cdd101 View Post
    Since I really seem to struggle with my teachers notes would you recommend a book or something to learn from?
    The C Programming Language - Wikipedia, the free encyclopedia seems to be a popular recommendation

    for more general problem solving, maybe http://en.wikipedia.org/wiki/Walls_and_Mirrors

  15. #60
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by SirPrattlepod View Post
    The C Programming Language - Wikipedia, the free encyclopedia seems to be a popular recommendation

    for more general problem solving, maybe Walls and Mirrors - Wikipedia, the free encyclopedia
    Thank you pal. Also Data Structure and Algorithms, is it true if you learn this you basically understand any language?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys, back again with a question
    By Velocity in forum C++ Programming
    Replies: 10
    Last Post: 10-19-2008, 02:27 PM
  2. hai guys help on 2-dim arrays
    By zerlok in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 12:58 PM
  3. OK. new easy question for you guys.
    By arnis in forum C++ Programming
    Replies: 8
    Last Post: 07-17-2003, 09:52 PM
  4. Ok. sorry to bother you guys with a newb question.
    By arnis in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2003, 11:23 AM
  5. Need to ask you guys a Question....
    By Halo in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-03-2003, 01:38 AM