Thread: Turing to C Programming

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    16

    Turing to C Programming

    I have to change this code from Turing to C language but I'm not sure how.

    Code:
    var a : array 1 .. 10 of intvar n, x, sum, offer : int
    var avg : real
    
    
    a (1) := 100
    a (2) := 500
    a (3) := 1000
    a (4) := 5000
    a (5) := 10000
    a (6) := 25000
    a (7) := 50000
    a (8) := 100000
    a (9) := 500000
    a (10) := 1000000
    
    
    get n
    for i : 1 .. n
        get x
        a (x) := 0
    end for
    sum := 0
    for i : 1 .. 10
        sum := sum + a (i)
    end for
    avg := sum / (10 - n)
    get offer
    if offer > avg then
        put "deal"
    else
        put "no deal"
    end if

    I can easily translate most of the program except for this part. I know its a loop, but how would you change this? :

    Code:
    get n
    for i : 1 .. n
        get x
        a (x) := 0
    end for
    sum := 0
    for i : 1 .. 10
        sum := sum + a (i)
    end for
    avg := sum / (10 - n)
    Help please?

    EDIT: Alright I somewhat figured this. The first loop is to check how many inputs are left. There are 11 inputs available in total. For ex, if a user enter 2 in the beginning, there are 10 inputs left to be taken from the user. Is there a way to program this command using a loop?
    Last edited by Zexious; 02-14-2012 at 08:16 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Just look at a normal for loop:

    Code:
    for (initialization; condition; increment) {
        statements;
    }

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    No but what I don't understand is where "i" came from, and the semi colon, along with ".."


    Sorry, I am somewhat a beginner at this

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I'm not familiar with the language, but I'm guessing it's one of these situations:

    Code:
    #include <stdint.h>
    
    /* ... */
    
    uint8_t i;
    
    for (i = 0; i < 5; i++) {
        /* your stuff */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Turing machine
    By Dante Wingates in forum General Discussions
    Replies: 5
    Last Post: 07-26-2010, 01:47 AM
  2. Turing Machine
    By Brad C in forum Tech Board
    Replies: 2
    Last Post: 08-05-2005, 08:22 PM
  3. Turing machines for the constipated
    By sean in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-06-2004, 11:14 PM
  4. help with zooming and turing camera
    By godhand in forum Game Programming
    Replies: 4
    Last Post: 05-25-2004, 11:39 PM
  5. turing
    By iain in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 12-14-2001, 10:05 AM