Thread: Print first element of array without using index

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    2

    Print first element of array without using index

    Hi everyone. I have to print array in this order 5,10,.., 30 (this isn't the problem using for loop), but after that i have to print first number, but when i print i, the result is the last number that came from last iteration in loop. I havent learn arrays, so the solution with indexes is not an option.
    Output should be like this: 5 10 15 20 25 30 5
    Last edited by Andrijana; 02-10-2020 at 03:32 AM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    If you haven't learned arrays then why are you expected to print an... array?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you meant to say that you're supposed to print a sequence, presumably of variable length? If so, one simple solution is to save the initial number in a separate variable, then after using the loop to print the sequence other than the last number, you print the initial number again.

    Of course, if the requirement is really for you to always print the sequence 5 10 15 20 25 30 5, then just save the trouble and write:
    Code:
    printf("5 10 15 20 25 30 5\n");
    (This follows from the programming dictum: do the simplest thing that could possibly work.)
    Last edited by laserlight; 02-10-2020 at 04:39 AM.
    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

  4. #4
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by Hodor View Post

    If you haven't learned arrays then why are you expected to print an... array?
    That. And

    It sounds like you already have code, posting it alongside your question would have been great

  5. #5
    Registered User
    Join Date
    Feb 2020
    Posts
    2
    Thank you for suggestions!

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Andrijana View Post
    Thank you for suggestions!
    I'm glad that's all cleared up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print the array element error in program
    By vead in forum C Programming
    Replies: 4
    Last Post: 01-06-2018, 06:11 PM
  2. Insert element after the last element in array in c
    By amaturequestion in forum C Programming
    Replies: 3
    Last Post: 04-09-2015, 08:29 AM
  3. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  4. how to print/access the 1st element of the first set of the vector
    By kapil1089thekin in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2010, 02:28 PM
  5. get the largest element from the array and print it
    By casperghost in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 06:22 PM

Tags for this Thread