#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Question Printing certain elements of an array


    So I have finally reached the arrays chapter. There's a beginner question I have for my first assignment. I have ten elements in an array, each representing 0, 1, 2... 9. They store Boolean values. Some are true, and some are false. How do I print out the elements which are true?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    Create the loop to iterate over array and print all elements.

    Then insert "if" statement inside, so that element is printed only if it satisfies your condition.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    Create the loop to iterate over array and print all elements.

    Then insert "if" statement inside, so that element is printed only if it satisfies your condition.
    Okay so this is a sample program:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int main(void)
    {
    	bool digit_seen[10] = {false};
    	int digit, i;
    	long n;
    	
    	printf("Enter a number: ");
    	scanf("%ld", &n);
    	
    	while (n > 0) {
    		digit = n % 10;
    		digit_seen[digit] = true;
    		n /= 10;
    	}
    	printf("\nRepeated digit(s): ");
    	for (i = 0; i < 10; i++)
    	printf("%d", digit_seen[digit] = true);
    	
    	return 0;	
    }
    What I am trying to do is, print the digits that occur more than once. I am not sure how to incorporate the if statement in the last part as you suggested. Could you please tell me how I should modify this code at the end? A demonstration would suffice. Thanks :)
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    you are trying to assign value inside printf? I am not sure it is a great idea. you need to use at least == here.

    I'm speaking of the following:
    Code:
    for (i = 0; i < 10; i++) {
        if (digit_seen[digit] == true) {
            printf("%d", digit);
        }
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    you are trying to assign value inside printf? I am not sure it is a great idea. you need to use at least == here.

    I'm speaking of the following:
    Code:
    for (i = 0; i < 10; i++) {
        if (digit_seen[digit] == true) {
            printf("%d", digit);
        }
    }
    Okay so here's my new code:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    int main(void)
    {
    	bool digit_seen[10] = {false};
    	int digit, i;
    	long n;
    	
    	printf("Enter a number: ");
    	scanf("%ld", &n);
    	
    	while (n > 0) {
    		digit = n % 10;
    		digit_seen[digit] = true;
    		n /= 10;
    	}
    	printf("\nRepeated digit(s): ");
    	for (i = 0; i < 10; i++) {
    	if (digit_seen[digit] == true) {
    		printf("%d", digit);
    		}
    	}
    	return 0;	
    }
    Now it behaves erroneously. Suppose I enter, "778834212", then it just prints a bunch(10) of 7s.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    you are trying to assign value inside printf? I am not sure it is a great idea. you need to use at least == here.

    I'm speaking of the following:
    Code:
    for (i = 0; i < 10; i++) {
        if (digit_seen[digit] == true) {
            printf("%d", digit);
        }
    }
    Okay now I have replaced digit with i:
    Code:
    for (i = 0; i < 10; i++) {
        if (digit_seen[i] == true) {
            printf("%d", i);
        }
    }
    What happens now is that it simply pings back all the digits entered for once. For example, if I enter "778834212", it prints out "123478".
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    you are trying to assign value inside printf? I am not sure it is a great idea. you need to use at least == here.

    I'm speaking of the following:
    Code:
    for (i = 0; i < 10; i++) {
        if (digit_seen[digit] == true) {
            printf("%d", digit);
        }
    }
    I suppose I have some problem with my while loop. I'll try to see what I can do with it. If you can figure out the problem, do let me know. Thank you :)
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    What happens now is that it simply pings back all the digits entered for once. For example, if I enter "778834212", it prints out "123478".
    But I thought it was exactly what you want :D

    I.e. show which digits were used.

    Perhaps, it is the time to describe what task you are trying to solve?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    But I thought it was exactly what you want :D

    I.e. show which digits were used.

    Perhaps, it is the time to describe what task you are trying to solve?
    I want to print out digits which have been repeated.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Okay I have solved it! Here's the code:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int digit_seen[10] = {0};
    	int digit, i;
    	long n;
    	
    	printf("Enter a number: ");
    	scanf("%ld", &n);
    	
    	while (n > 0) {
    		digit = n % 10;
    		if (digit_seen[digit]) {
    			digit_seen[digit] = 2;
    		}
    		else	{
    		digit_seen[digit] = 1;
    		}
    		n /= 10;
    	}
    	
    	printf("\nRepeated digit(s): ");
    	for (i = 0; i < 10; i++) {
    	if (digit_seen[i] == 2) {
    		printf("%d", i);
    		}
    	}
    	return 0;	
    }
    And it works just as expected! Thanks for helping me with the if statement. :)
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    Yes, you found proper solution. Though perhaps you need a small fix to make it work with numbers like "333". :)

    By the way, what you have learnt is a main part of the "counting sort".
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    Yes, you found proper solution. Though perhaps you need a small fix to make it work with numbers like "333". :)

    By the way, what you have learnt is a main part of the "counting sort".
    How about this one?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int digit_seen[10] = {0};
    	int digit, i;
    	long n;
    	
    	printf("Enter a number: ");
    	scanf("%ld", &n);
    	
    	while (n > 0) {
    		digit = n % 10;
    		digit_seen[digit]++;
    		n /= 10;
    	}
    	
    	printf("\nRepeated digit(s): ");
    	for (i = 0; i < 10; i++) {
    	if (digit_seen[i] > 1) {
    		printf("%d", i);
    		}
    	}
    	return 0;	
    }
    This seems to work fine!
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    Congratulations! Exactly so!

    BTW: also I dare to advise you put more attention to style of the code - it will become important as your skills and your programs grow. Especially now here are problems with indentation - though in C indentation is not crucial like in Python - but it greatly helps in comprehension of program structure.

    Moreover, neglecting code style is the best way to lose the job if you have one or not to get one if you have not :)

    There are a lot of conventions for C/C++, unlike for java, so you may choose which you like more. For example:

    http://google-styleguide.googlecode....k/cppguide.xml
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by rodiongork
    Congratulations! Exactly so!

    BTW: also I dare to advise you put more attention to style of the code - it will become important as your skills and your programs grow. Especially now here are problems with indentation - though in C indentation is not crucial like in Python - but it greatly helps in comprehension of program structure.

    Moreover, neglecting code style is the best way to lose the job if you have one or not to get one if you have not :)

    There are a lot of conventions for C/C++, unlike for java, so you may choose which you like more. For example:

    http://google-styleguide.googlecode....k/cppguide.xml
    This link is an awesome resource! I'll read through it and try to follow the conventions. Thanks again sir :)

IMN logo majestic logo threadwatch logo seochat tools logo