Thread: Help with arrays task

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    2

    Help with arrays task

    I have just started learning about arrays, however I cant get my head around them, I am trying to write code that does this:

    Asks the user to enter 5 different characters and stores these in an array
    Prints the array on screen
    Asks the reader to enter 5 numbers between 1 and 10 into a second array
    Prints the second array on screen
    Prints out all characters in array 1 the number of times indicated by the entry in array 2

    So far I have come up with this:
    Code:
        char inputOne[5], i=0;
        int inputTwo[5], j=0;
    
    
        printf("Enter 5 different characters: ");
    
    
        for ( i=0; i<5; ++i){
    
    
            scanf("%s", &inputOne[i]);
    
    
        }
    
    
        printf("You entered: %c, %c, %c, %c, %c", inputOne[0], inputOne[1],inputOne[2],inputOne[3],inputOne[4]);
    
    
    
    
        printf("\nEnter 5 numbers between 1 and 10: ");
    
    
        for ( j=0; j<5; ++j){
    
    
            scanf("%d", &inputTwo[j]);
    
    
        }
    
    
        printf("You entered: %d, %d, %d, %d, %d", inputTwo[0], inputTwo[1],inputTwo[2],inputTwo[3],inputTwo[4]);
    however this doesnt include the last bit as I am struggling to work out how to do it, could anyone help me out

    an example of what the input and output should be:

    User enters: b, e, f, a, x into array 1
    2, 5, 6, 1, 9 into array 2

    Output: You entered: b, e, f, a, x into array 1
    You entered: 2, 5, 6, 1, 9 into array 2

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,548
    %s is the format specifier to read a string. Since you want to read a character only, it should be %c.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    2
    okay but do you know how i would complete the last point 'Prints out all characters in array 1 the number of times indicated by the entry in array 2'

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,548
    Yes: loop through both arrays at the same time. You need an inner loop too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help from the task
    By MAJA83 in forum C Programming
    Replies: 1
    Last Post: 06-06-2012, 11:35 AM
  2. Need help to do the big task
    By Steve Cao in forum Linux Programming
    Replies: 1
    Last Post: 07-16-2010, 02:01 PM
  3. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  4. task bar
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-21-2002, 05:53 PM
  5. Using The 'Task Bar'?!?!
    By minime6696 in forum Windows Programming
    Replies: 2
    Last Post: 01-13-2002, 04:30 PM