write algorithm

Discussion in 'C' started by hnaya, Nov 29, 2008.

  1. hnaya

    hnaya New Member

    Joined:
    Nov 29, 2008
    Messages:
    9
    Likes Received:
    3
    Trophy Points:
    0
    i have difficulty to write algorithm to following programs in c language
    any one can help me ??



    PROGRAM TO FIND SUM OF INDIVIDUAL DIGITS OF A NUMBER
    PHP:
    #include <stdio.h>
    #include <conio.h>

    void main()
    {  
    int a,n,final,sum=0;  
    clrscr();  
    printf("SUM OF DIGITS OF A NUMBER\n");
    printf("-------------------------\n\n");
    printf("Enter the number : ");  
    scanf("%d",&a); 
    final=
    a
    do  {    
        
    n=a%10;    
        
    sum=sum+n;    
        
    a=a/10;  
        }  
    while(
    a!=0);  
    printf("\nThe sum of digits of the number %d is %d",final,sum); 

    printf("\n\nPRESS ANY KEY TO EXIT..."); 
    getch();
    }



    PROGRAM TO FIND SUM AND AVERAGE OF GIVEN NUMBERS

    PHP:

    #include <stdio.h> 
    char input[1000]; 
    int num_count
    float totalaveragetemp
    int main() 


    printf("FIND SUM & AVERAGE OF GIVEN NUMBERS\n"); 
    printf("-----------------------------------\n\n\n"); 
    printf("Enter first number (00 to quit): "); 

    fgets(inputsizeof(input), stdin); 
    sscanf(input"%f", &temp); 

    num_count 0
    total 0.0
    average 0.0

    while ( 
    temp != 00.00 ) { 
    ++
    num_count
    total += temp;  
    temp 0

    printf("Enter next number (00 to quit): "); 
    fgets(inputsizeof(input), stdin); 
    sscanf(input"%f", &temp); 

    average = ( total num_count ); 
    printf("\nYou have entered %d numbers. \n\nThe SUM of those numbers is %f. \nThe AVERAGE of those numbers is %f.\n"num_count,totalaverage); 

    printf("\n\n PRESS ANY KEY TO EXIT...");
    getch();
    return 
    0
    }



    third program witch FIND LCM AND GCD OF TWO INTEGERS
    PHP:

    #include <stdio.h>

    int gcd(int m,int n)
    {
    while( 
    m!= n
      {
        if( 
    n)
          
    mn
        else
          
    nm;
      }
    return (
    m); 
    }
    int lcm(int m,int n)
    {
    return( 
    gcd (n)); 
    }

    main()
    {
    int m,n
    //int lcm(int,int);
    //int gcd(int,int);
    printf("\n LCM & GCD FINDER \n");
    printf(" -----------------\n\n");
    printf("\nEnter the first number : ");
    scanf("%d", &m);
    printf(" \nEnter the second number : ");
    scanf("%d", &n);

    printf("\n\nGCD of %d and %d is %d\n",m,ngcd(m,n));
    printf("LCM of %d and %d is %d\n",m,nlcm(m,n));

    printf("\n\nPRESS ANY KEY TO EXIT...");
    getch();
    }


    PROGRAM TO FIND THE REVERSE OF A NUMBER

    PHP:

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int x,y,z;
    printf("REVERSING A NUMBER \n");
    printf("------------------\n\n");
    printf("Enter the number to be reversed : ");
    scanf("%d",&x);
    if(
    x<0
     {
       
    x=x*(-1);
     }
     
    printf("\n\nThe reversed number is: ");
    while(
    x>10)
     {
       
    y=x/10;
       
    z=x%10;
       
    x=y;
       
    printf("%d",z);
     }
    printf("%d",x);
    printf("\n\nPRESS ANY KEY TO EXIT...");
    getch();
    }

    PROGRAM TO FIND SUM OF ROW< COLUMN ELEMENTS AND ALL ELEMENTS OF A 3x3 MATRIX

    PHP:
    #include<stdio.h>
    void main()
    {
    int matrix[3][3];
    int i,j,x,y,total;
    int sum=0;
    printf("MATRIX OPERATIONS \n");
    printf("----------------- \n\n");
    printf("Enter the 3x3 Matrix elements: \n");
    for(
    i=1;i<=3;i++)
    {
        for(
    j=1;j<=3;j++)
        {     
    scanf("%d", &matrix[i][j]);   }
    }
    clrscr();
    printf("The matrix you entered is : \n");
    for(
    i=1;i<=3;i++)
    {
        for(
    j=1;j<=3;j++)
        {  
    printf("%d     "matrix[i][j]);  }
    printf("\n\n");
    }
    for(
    i=1;i<=3;i++)
    {
        for(
    j=1;j<=3;j++)
        {  
    sum sum matrix[i][j];  }
    printf("Sum of Row %d is : %d\n"i,sum);
    sum=0;
    }

    printf("\n\n");
    for(
    i=1;i<=3;i++)
    {
        for(
    j=1;j<=3;j++)
        {   
    sum sum matrix[j][i];   }
    printf("Sum of Column %d is : %d\n"i,sum);
    sum=0;
    }
     

    printf("\n\n");
    for(
    i=1;i<=3;i++)
    { for(
    j=1;j<=3;j++) 
     {  
    total total matrix[i][j];    }
    }
    printf("Sum of all elements of the matrix is : %d\n"total);
    printf("\n\nPRESS ANY KEY TO EXIT...");
    getch();
    exit(
    0);
    }



     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well, let's start with the first one. What does it do, and how does it do it? Can you explain in English how it works? From that we can then go on to write the algorithm.
     
  3. hnaya

    hnaya New Member

    Joined:
    Nov 29, 2008
    Messages:
    9
    Likes Received:
    3
    Trophy Points:
    0
    this is explanation for each program

    1. To find the sum of individual digits of a given integer number .
    2. To find the sum and average of a given array of integer numbers .
    3. To find the GCD and LCD of given two integer numbers .
    4. To find the reverse of a given integer number .
    5. To find the sum of row elements , column elements and all elements of given 3x3 matrix of non-zero integers .


    .
    also i attached word file contain the program
    i want the algorithm for each one
     
    Last edited by a moderator: Nov 30, 2008
  4. neo_vi

    neo_vi Member

    Joined:
    Feb 1, 2008
    Messages:
    720
    Likes Received:
    16
    Trophy Points:
    18
    Occupation:
    Software engineer
    Location:
    Earth
    Home Page:
    http://computertipaday.blogspot.com
    writing algorithm isn't that much difficult. for e.g for the first program
    step1: initialize the variables
    step2: modulo divide the number by 10 so as to get the individual digits.
    step3: save the number in a variable.
    step4: divide the number by 10.
    ......
    Like this u can move on and since it is in a loop, U specify that "follow these steps until u get a single digit" r u can even use goto <step number>.
    This may not be the actual or proper algorithm for ur program. I just wrote it for an example.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I'd say the algorithm is at a higher level than that, hence the suggestion to start with an English description of what it does.

    Note by "an English description of what it does" I don't mean just rehash the title. "To find the sum of individual digits of a given integer number" is just a title rehash, but the algorithm is how it does that.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Attachment removed and try to have the content in the post.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    hnaya, just one question: do you actually understand what the code is doing?
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    hnaya, an observation. You opened with "i have difficulty to write algorithm to following programs in c language ". I asked: "What does it do, and how does it do it?" and in answer you said "i want the algorithm for each one"

    Yes, I know you want the algorithm for each one.

    No, I'm not going to write it for you. (Someone else might though.)

    But I will help you write it, if you're willing to learn. So please answer my first question: "What does it do, and how does it do it?" (You may need to mention if you haven't got a clue how it works though; if you don't know that, you're definitely not going to be able to write the algorithm.)

    Lots of people post homework assignments expecting us (a) to write it for them and (b) not to be smart enough to realise that's what we're doing and (c) trustworthy enough that if we do realise, we won't write something for you that is plausible but significantly wrong. If that's what you're doing, you're not the first, you won't be the last, and it's very easy to spot (the key giveaways are the lack of willingness to learn and a "just give me the fscking answer" attitude).
     
  9. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    The best way to learn algorithm is I think, to work it out in your khata....
    At first, I also emphasised on writing the entire program...but what is more essential is to understand writing the algorithm....the other parts will come automatically...
    Just as xpi0t0s said...what will you do if you are not provided with a pc, just a notebook, a pen and you have been asked to draw a flow-chart of the key algorithm.

    Note that in the first program, the key algorithm is to distinguish the digits of a number....now how many ways can you do that.....draw all the possible flow-charts...and always stay greedy of drawing one more...then I think it will be perfect.

    Similarly, try to find out what the other key algorithms are in the remaining programs.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice