Thread: C programming HELP

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    Question C programming HELP

    Someone please help. I need help converting the following 2 algorithms into C.


    Dollar_Manipulator
    Set cash(index) to 10*
    Set num to 2
    Repeat
    Add 1.5 to cash(num)
    Add 2 to num
    Until(num == 10)
    Set num to 1
    Repeat
    If (num%3 == 0) Then
    Subtract 3.3 from cash(num)
    EndIf
    If (num%4 == 0) Then
    Multiply cash(num) by 2
    EndIf
    Until (num == 10)
    Prompt for num
    Get num
    Set lucky_num to (num%10)
    Set num to 1
    Repeat
    If (num == lucky_num) Then
    Do Nothing
    Else
    Add cash(num) to cash(lucky_num)
    EndIf
    Until (num == 10)
    Display cash(index)*
    End

    *Note that index = 10. You are declaring, initializing, and displaying the array of 10 locations numbered 1 to 10. Make necessary changes in C to reflect this.

    Misc_Program
    Set MAX to 10
    Set MID to 5
    Set MIN to 1
    Do index = MIN to MAX
    Set array(index) to 0
    Display array(index)
    If (array(index) == 2 Or 4 Or 6 Or 8) Then
    Add index— to array(index)
    Else
    Add index to array(index)
    EndIf
    EndDo
    Display modified array message
    Do index = MIN to MAX
    Display array(index)
    EndDo
    End




  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    what exactly do these algorithms do?

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    for setting the flags here is what you do
    Code:
    int flag;
    //for the flag is set
    flag = 1;
    //for unsetting the flag
    flag = 0;
    //for conditions if flag is on
    if(flag)
    {
    	//...
    }
    else
    {
    	//...
    }
    //etc.
    what I mean by flags is the "action" or variable you used SET on.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This reeks of homework, so I'll offer tips instead of code. In the case of the Repeat..Until statements, consider using a while loop. cash(index) is an array, so you can simply change the notation slightly to cash[index] to convert it to C. You might want to make use of the arithmetic assignment operators that C provides, such as +=, -=, and *=. Perhaps a for loop would be appropriate for the Do construct. Everything else is simple enough for you to figure out with a little effort.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    aha

    prelude---can you elaborate more
    " programming is 1% syntax and 99% logic "

Popular pages Recent additions subscribe to a feed