Thread: Problem in program

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    Problem in program

    I want to print Characters 'X','Y','Z' in reverse way 'Z','Y','X'

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        char charcters[3] = {'X','Y','Z'}; 
    	
    	unsigned int index;
    	
    	for (index =0; index < 3; index++)
    	{
    		printf("Characters  %c \n", charcters[index]);
    		
    	}
    	
    	for (index =3; index > 0; index--)
    	{
    		printf("Characters in reverse way %c \n", charcters[index]);
    		
    	}
        
    	return 0;
    }
    Characters X
    Characters Y
    Characters Z

    Characters in reverse way 
    Characters in reverse way Z
    Characters in reverse way Y

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Also print the value of index in both loops.

    Then you will see you're off by 1 when counting backwards.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Salem View Post
    Also print the value of index in both loops.

    Then you will see you're off by 1 when counting backwards.
    I did it
    Code:
    #include <stdio.h> 
     int main(void)
    {
    	char charcters[3] = {'X','Y','Z'}; 
    	int index;
    	for (index = 0; index < 3; index++)
    	{
    		printf(" Characters  %d = %c \n",index, charcters[index]);
    	}
    	
    	for (index = 2; index >= 0; index--)
    	{
    		printf(" reverse way %d = %c \n",index,charcters[index]);
    	}
        return 0;
    }
    Characters 0 = X
    Characters 1 = Y
    Characters 2 = Z

    reverse way 2 = Z
    reverse way 1 = Y
    reverse way 0 = X

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Program to find maximum number in three numbers

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 25;
    	int b = 35;
    	int c = 15;
    	int maximum;
    	
    	maximum = 25;
        
        if (maximum < b)
    	{
    		maximum = a;
    	}
    	if (maximum < c)
    	{
    	    maximum = c;
    	}
    	printf("\n maximum number %d ", maximum);
    	
    	return 0;
    }
    maximum number 25

    Is there any mistake ?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by vead View Post
    Program to find maximum number in three numbers

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a = 25;
    	int b = 35;
    	int c = 15;
    	int maximum;
    	
    	maximum = 25;
        
        if (maximum < b)
    	{
    		maximum = a;
    	}
    	if (maximum < c)
    	{
    	    maximum = c;
    	}
    	printf("\n maximum number %d ", maximum);
    	
    	return 0;
    }
    maximum number 25

    Is there any mistake ?
    Yes.

    The first clear mistake is this line.

    Code:
    maximum = 25;
    I would suggest making it
    Code:
    maximum = a;
    There is also a worse second mistake; I did not look for a third mistake.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by stahta01 View Post
    Yes.

    There is also a worse second mistake; I did not look for a third mistake.

    Tim S.
    What is that ?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by vead View Post
    What is that ?
    Do your own homework!!
    If you tried running the program then the problem would be clear.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by stahta01 View Post
    Do your own homework!!
    If you tried running the program then the problem would be clear.

    Tim S.
    I have run program and result already posted. By the way this is not homework. I asked to improve program writing style. this program is written by me

  9. #9
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Easiest is to write a function to determine the max of two values, like so:
    Code:
    int max_val(int a, int b)
    {
      /* return your result here */
    }
    
    int main()
    {
      int a = 5, b = 7, c = 2;
      
      int d = max_val(max_val(a, b), max_val(a,c));
    
      printf("%d", d);
    
    }

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by vead View Post
    I have run program and result already posted. By the way this is not homework. I asked to improve program writing style. this program is written by me
    And, you did not realize that 25 was smaller than 35?

    Code:
    int b = 35;
    Now where in the code is the max value set to the value of "b"?
    Answer, never.

    Tim S.
    Last edited by stahta01; 01-26-2018 at 12:42 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by stahta01 View Post
    Now where in the code is the max value set to the value of "b"?
    Answer, never.

    Tim S.
    Thank you very much.

    Code:
    #include<stdio.h>
    
    int main (void)
    {
    	int a =25; 
    	int b=35; 
    	int c=15; 
    	int maximum = a;
         
        if (maximum < b)
        {
            maximum = b;
        }
        if (maximum < c)
        {
            maximum = c;
        }
        printf("\n maximum number %d ", maximum);
         
        return 0;
    }
    maximum number 35

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in my first program :/
    By RattleSlash in forum C Programming
    Replies: 15
    Last Post: 09-13-2011, 03:05 PM
  2. Problem with C Program
    By TheEraserGI in forum C Programming
    Replies: 9
    Last Post: 07-20-2011, 12:30 PM
  3. Problem with my program...
    By saya_makan36 in forum C Programming
    Replies: 1
    Last Post: 03-09-2010, 06:49 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM

Tags for this Thread