Thread: Can you help me with this.. please...

  1. #1
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16

    Unhappy Can you help me with this.. please...

    Hi to all C enthusiasts,

    Greetings!

    My name is Alexander, a C enthusiast, 17-year-old, and live in Bacolod City, Negros Occidental Philippines. I have given my brief info and this is my first time to post here. My problem is, I have coded an output like this:
    Code:
    Enter num1: 1
    Enter num2: 1
    Enter num3: 3
    Enter num4: 5
    Enter num5: 3
    
    Number 1 appeared 2x
    Number 3 appeared 2x
    Number 5 appeared 1x
    I tried it with this code..
    Code:
    #include <stdio.h>
    #include <conio.h>
    void main() {
    	int num[5] = {0};
            int x, y, temp;
    	clrscr();
    
    	for(x = 0;  x <= 4; x++) {
    		printf("Enter num%d: ", x+1);
    		scanf("%d", &num[x]);
    	}
    	printf("\n");
    
    	for(x = 0; x <= 3; x++) {
    		for(y = x + 1; y <= 3; y++) {
    			if(num[x] > num[y]) {
    				temp = num[x];
    				num[x] = num[y];
    				num[y] = temp;
    			}
    			printf("\n");
    
    			int h = 0;
    			x = 0;
    			y = 1;
    			while(x <= 4) {
    				h = 1;
    				printf("Number %d ", num[x]);
    				while(num[x] == num[y]) {
    					y++;
    					h++;
    					x++;
    				}
    				printf("appeared %dx\n", h);
    				if(h == 1) {
    					x++;
    					y++;
    				} else {
    					x = y;
    					y++;
    				}
    			}
    		}
    	}
    	getch();
    }
    
    
     But the Output is:
    
    Enter num1: 1
    Enter num2: 1
    Enter num3: 3
    Enter num4: 5
    Enter num5: 3
    
    Number 1 appeared 2x
    Number 3 appeared 1x
    Number 5 appeared 1x
    Number 3 appeared 1x
    Please anyone, evaluate this problem and post me your responses so that it will help enhance my understanding in arrays and loops.

    Thank you in advance..

    Cordially Yours,

    Alexander Daguob
    Student, BSCOE - I, AMACC - B

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    It seems to me if you made this, you have understood how to loop! rofl. I could make this process alot easier for you if you would want my brief version of how to do it, but I'm not analyzing that for you, that's your job
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Hi pre

    So you want someone to comment the code for you?
    The code is too long for its problem, theres is a too short version of that

  4. #4
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    Quote Originally Posted by loko
    Hi pre

    So you want someone to comment the code for you?
    The code is too long for its problem, theres is a too short version of that
    Hello po sir,

    Opo.. gusto ko lang po malaman ang simple at tamang code dahil yung mga code na na-post ko po ay ang nakaya lang ng logic ko...
    Sana po tolongan nyu ako...
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    haha bro i dont think they allow any spkoen language here other than english. So lets talk in english.

    This is all you need.
    Code:
    	
    
    //initializing data
    for(x = 0;  x <= 4; x++) {
    		printf("Enter num%d: ", x+1);
    		scanf("%d", &tmp);
                    num[tmp-1]++; // Increment index for 3.
    }
    
    //printing output..
    for( x = 0; x <= 4; x++ ){
        if( num[x] > 0 )
            printf(  Number %d appeared %dx ", x + 1, num[x] );
    }
    The code above hasnt been tested. But im sure it will work .

  6. #6
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    Sir Loko,

    Woah! It had worked quitely..

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main() {
    	int x, tmp, num[5] = {0};
    	clrscr();
    	//initializing data
    	for(x = 0;  x <= 4; x++) {
    		printf("Enter num%d: ", x+1);
    		scanf("%d", &tmp);
    		num[tmp-1]++; // Increment index for 3.
    	}
    	printf("\n");
    	//printing output..
    	for( x = 0; x <= 4; x++ ){
    		if( num[x] > 0 )
    			printf("Number %d appeared %dx\n", x + 1, num[x]);
    	}
    	getch();
    	return 0;
    }
    How come I didn't think of using the memory locator of the scanf as the index of my array... perhaps I'm only a newbie C enthusiast...lolz
    I, however, am thanking you sir for your effort in giving me a simple code..
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  7. #7
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    No Problema . And dont call me sir we are almost the same age

  8. #8
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16

    Getting to know loko.

    Okay..
    Anyway, what school are you in? Your name? Age? Your Course?
    Last edited by Alexthunder; 10-05-2005 at 07:24 PM. Reason: I've forgot to write the course..
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  9. #9
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Alexthunder
    Okay..
    Anyway, what school are you in? Your name? Age?

    Im from STI somewhere in Southern Tagalog. Im a second Year Student. My age?? Secret .

  10. #10
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16
    and your course and name?
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  11. #11
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Alexthunder,

    If you must ask other forum members about their personal information, why don't you private message them? It doesn't have anything to do with C.

  12. #12
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Im in a 2 years computer science course. My name is Mal.

    Lets talk sa Yahoo sometime. PM me your Yahoo.

  13. #13
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by cwr
    Alexthunder,

    If you must ask other forum members about their personal information, why don't you private message them? It doesn't have anything to do with C.
    Sorry about that.

    Peace

  14. #14
    Dyadic Alexthunder's Avatar
    Join Date
    Sep 2005
    Location
    Philippines
    Posts
    16

    I am sorry

    Quote Originally Posted by cwr
    Alexthunder,

    If you must ask other forum members about their personal information, why don't you private message them? It doesn't have anything to do with C.
    Sir, sorry, I'm a newbie here in cboard forums, I didn't know sir how to pm him so I've posted it in here... don't worry sir it won't happen again..
    An apprentice carpenter may want only a hammer and saw, but a master craftsman employs many precision tools. Computer programming likewise requires sophisticated tools to cope with the complexity of real applications, and only practice with these tools will build skill in their use. Robert L. Kruse

  15. #15
    Registered User 2600's Avatar
    Join Date
    Mar 2005
    Posts
    2
    Click on USER CP there you will find option for sending messages.

Popular pages Recent additions subscribe to a feed