Display words at random

This is a discussion on Display words at random within the C Programming forums, part of the General Programming Boards category; Hi, im a total newbie in c programming. i got this assignment and dont know how to do it. if ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Display words at random

    Hi, im a total newbie in c programming. i got this assignment and dont know how to do it. if anybody could help it would be greatly appreciated.

    Code:
    char commonWords[13][50] =      
         { "It was Thursday.",
            "We were looking forward to Friday and the weekend",
            "that follows. The wind continued to blow from",
            "Sumatra and the air pollution index, API, went up",
            "and up. In the afternoon the ministry announced",
            "that all schools be closed for the rest of the",
            "day as well as Friday. Cancellation of classes",
            "has always been the best thing that can ever",
            "happen to students and lecturers. On this day,",
            "the API told us that it would take a fool to be",
            "happy at all. Friday evening, a horrifying sight",
            "hit me. Our beautiful pools had turned brownish",
            "green."
          };
    given the code i need to convert them into a 1-dimensional array of single-worded strings at the start of program execution. then generate a random number within the appropriate range and uses that number as the index to the word to be displayed. The word is to be displayed with its letters in reverse order.

    example of output :
    Code:
    Display words at random
    --------------------------
     
       Random number:  5
    
       Word at that index: olleh
    Please help thank you !!

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    The policy here is to not provide any raw solution to a problem.
    You have to work on it, show some code, and ask a specific question about a coding issue you have.

    If it's an assignment you surely have some requirement? things you can use or not? You can also start by identifying sub problems you have like reversing a string and printing it, etc.
    If you can't do the first part, i.e. splitting the array, then assume you've done it and do the rest.
    At least it will give you some hint or the general design of your program.
    I suggest you check strtok() to split the string into words, but there are others ways.
    Last edited by root4; 09-09-2012 at 02:50 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    I think the first bit will be hardest - splitting it into words.

    Have a look at strtok - C++ Reference - you can use this to break the sentences into words. Properly, you should do a dry run through the array to find out how many words there are, then create an array of the appropriate size, but you can always do that later and start with a fixed length array.

    I like root4's suggestion of starting with something other than the first part. The latter parts seem like they should help you understand what you need to do with the array. Then you can come back with some code

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome saejima!

    Just a small tip: you'll want to include string.h, and stdlib.h into your header files for the program. This program will take some time, so start on it, and post up some code (however humble a start it may be), without delay.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2011, 03:54 AM
  2. Display selected words from text file
    By picasso in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2007, 09:06 AM
  3. generating random words
    By lilrayray in forum C Programming
    Replies: 24
    Last Post: 07-24-2006, 07:11 PM
  4. Picking out random words
    By beanroaster in forum C++ Programming
    Replies: 16
    Last Post: 09-04-2005, 04:02 AM
  5. Replies: 1
    Last Post: 12-14-2002, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21