Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C programming guidance

  • 04-01-2016 4:37pm
    #1
    Registered Users Posts: 14


    Hi all, was recommended by a friend to come on here to see if anyone could help or even point me in the right direction with a c programming problem I'm having. I had an assignment due in college today for 2 pm but was only able to get 1 of 2 programs wrote up. I can't seem to get my head round the 2nd one. It sounds very simple but trust me I've spent the bones of 30 hrs trying to get this over the xmas.
    What I have to do is take a string of letters and swap them with other letters to print out a statement.
    For example a = f
    B =v
    I'm not asking for someone to write the program for me but maybe shed a bit of light on it and maybe tell me which statement or back bone of the program to be.
    Thanks so much of any one could help
    Patrick


Comments

  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Is it possible to post the full assignment description? And also your work that you've done 30 hours on already.

    From what I can make out though it looks like you're being asked to encode some text using a substitution cipher, but it seems to be an arbitrary one where 'a' needs to be encoded to 'y' and 'b' to 'n'. In other words the plain text is being encoded using a substitution cipher where the substitution alphabet is in an arbitrary order...

    If that's the case then use an array of 26 elements to store the arbitrarily ordered letters to be substituted in place of the plain text letters. In this array the 0th element is plain text a and the 25th element is plain text z.

    When you are looking to encode a plain text character you just need simple character arithmetic to get the correct index of the cipher character array. Eg cipher_chars[plain_text_char - 'a'] will access the correct cipher character for the plain text character being encoded.

    That's a simplified explanation and I'm not going into any more detail in case I have misunderstood the brief. You may need to play around a bit with changing cases to make this work, but I'll leave that up to you.


  • Registered Users Posts: 14 Pac8


    Thanks very much bad I'm at this min still working on it. Your right I have a bunch of over 1000 letters all mixed up and I have a frequency of the alphabet accurances from A to Z. I have to take the most frequent occurring letter and substitute it with the most frequently used letters in the group of letters and so on with the most 2nd frequent. I can send u the assignment if u like. I have the first part done but still struggling to write the main body of the program


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Attach it here and I'll take a look.

    So now it sounds like you're making a program to automate the decoding of caeser cipher style encoded cipher text, very good...


  • Registered Users Posts: 14 Pac8


    Ya I've tried run it with a Caesar code but it won't run because letters are too mixed up.
    Thanks for looking I'll send it on


  • Registered Users Posts: 14 Pac8


    it wont let me attach anything to this
    iv even tried copy and pasting


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Can you share it a different way dropbox link, Google drive etc...

    Protip: have you tried changing the extension of the files you're trying to attach, I think that worked for me last week. Just add .txt at the end of the current file name...


  • Registered Users Posts: 14 Pac8


    I think I just sent it to u by a message


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Pac8 wrote: »
    I think I just sent it to u by a message

    Got it, when you say you have the first part done do you mean this: "One that determines the number of occurrences for each letter"


  • Registered Users Posts: 14 Pac8


    ya I have that bit done. that's where I have got that the occurance of the letters in the text match the letters that he has given with the % beside them. I wrote out the text swapping the letters last night and it ciphers into a paragraph about an invasion


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    How are you storing them values?
    Have you covered sorting yet?

    The simplest solution that occurs to me is to set up an array that has been manually sorted using the figures provided by your lecturer, so element 0 = 'e' 1 = 't' etc.

    I'm assuming that you have an array of 26 floats storing the calculated frequencies for this next part such that element 0 is the calculated frequency of the occurrence of the letter a in the cipher text. If that is the case then you need to sort that array, but first you have to set up a character array with the same number of elements and store the letters 'a', 'b', 'c'... etc in order and any time you swap two elements while sorting the frequencies array you also swap the same elements from the alphabetically ordered character array.

    You will then end up with two character arrays where the letter stored in element 0 of the manually sorted array is the plain text equivalent of the letter stored in element 0 of the other character array.

    Does any of that make sense? It is a bit tricky to be sure...


  • Advertisement
  • Registered Users Posts: 14 Pac8


    No haven't done sorting yet.
    What your saying does make a little sense. Strn1 can be the letters he gave and strn2 can be my text. But can I put both in manually after the strn1 and strn2 ie strn1 = " 0 = a, 1 = b etc.
    Will they still read each other off if i did a for statement for strn1 1 == strn2 g???
    Sure is a tough one to write


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Okay, since you haven't done sorting then my next solution requires using a switch statement, have you covered that?


  • Registered Users Posts: 14 Pac8


    ya we have done switch statement and I did try to use it but I was thinking there had to be an easier way. Also with the switch statement u can only print out one case at a time(I'm prob wrong saying this as this is all we have learned in switch)


  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Are you still here?

    From what you sent me the cipher text is all lower case? Either way switches are versatile enough for both cases.

    Right this back and forth takes too long do you have gmail for hangouts or something?

    If you're in bed though the solution I'm thinking of is use the first program to print out the frequencies of occurrence of letters in the cipher text. Then in the second program hard code the switch statement so that when the cipher text character that was calculated as occurring 10.206358% of the time is encountered the switch statement will print 'e'. Then iterate through the ciphertext one character at time use each character as input into the switch statement which should then print the appropriate plain text letter.


  • Registered Users Posts: 14 Pac8


    Hey how are u
    Tried last night strncmp but ended up no where. Was thinking to try match the percentages before but then u would have to write variables for percentages letters and match them first then match them to the text. Would be just the same as trying letter to letter. This bloody thing is doing my head in.
    Yesterday I was so bad I googled live c program chat and was talking to a lad in India I think and he said he could do it but for 50 dollars...half tempted now


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    That kind of defeats the purpose of getting an education. Also be aware that there's a very good chance your lecturers are on boards.ie, perhaps even watching this forum.


  • Registered Users Posts: 14 Pac8


    There's always one idiotic reply.
    U obviously didn't read my first text. All I'm asking for is guidance. I never stated I wanted someone to do it for me. I had an opportunity to have someone do it for me but like what I said I didn't take it.
    Thanks for your wise input tho.


  • Registered Users, Registered Users 2 Posts: 6,237 ✭✭✭Talisman


    Have you encountered the struct data type declaration? What about pointers? They could be used to solve the problem more efficiently.

    Integers are faster and easier to work with than floating point data types. Instead of using a float to store the percentage frequency consider using an unsigned int (0 to 4,294,967,295). Decide on the decimal place accuracy you want (up to 8/9 decimal places) for example accuracy to 6 decimal places (float) can be achieved by multiplying the value by 1,000,000.

    10.206358 * 1000000 = 10206358


  • Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭bpmurray


    I just realise you have this twice - see my response in the other thread: http://www.boards.ie/vbulletin/showthread.php?t=2057544488


  • Registered Users Posts: 14 Pac8


    Thanks guys for all the help. Unfortunately I didn't meet the deadline to have the assignment up but I'll def keep all info for next semester.

    All post and and advice will def go a long way
    Thank you bpmurray...works perfectly


  • Advertisement
Advertisement