Sequence of numbers

This is a discussion on Sequence of numbers within the C Programming forums, part of the General Programming Boards category; How can i easily produce a sequence of numbers? I would like to produce a sequence between 1 and 10 ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Sequence of numbers

    How can i easily produce a sequence of numbers?

    I would like to produce a sequence between 1 and 10 in 2's and I'm not sure how to do this.

    Can anyone help please?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Use a for loop with an increment of 2.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    The user specifies the length of the sequence (length) ie to 10 and also times, which is how many they want it to go up i.e 2. Sequence is how many values should be in the sequence.

    I have so far:

    Code:
    int length;
    int times;
    int sequence;
    int w;
    
    /*user specifies length times ,*/
    sequence=length/times;
    
    for(w=1;w<=sequence;w++)
    {
    NOT SURE +times
    }
    I don't know what to put inside the loop (where not sure is) so I get the right thing out. please help!
    Last edited by emj83; 02-27-2009 at 05:39 AM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Ok, a couple comments... It seems to me that length and sequence seem to be the same thing in your code...

    The user could specify the following for your problem...
    1. Starting number
    2. Ending Number
    3. Number of Numbers in the sequence
    4. The incremental value

    #2 and #3 are kind of mutually exclusive. In other words if they specify the ending number then specifying the number of numbers in the sequence doesn't make sense (unless you code it so that whichever happens first or last or something).

    I'm assuming for the code below, that the user is specifying ONLY the number of numbers in the sequence...

    Code:
    int start_num = 0;
    int curr_num = 0;  //Holds the current number
    int num_count = 0; //Number of numbers in the sequence
    int increment_num = 0;
    int x = 0; //Counter
    
    // Get user input
    
    curr_num = start_num;
    for (x = 1; x <= num_count; x++) {
      printf("%d\n", curr_num); // Print each number on it's own line
      curr_number += increment_num;
    }
    The code would be slightly different if you want the user to specify the ending number in the sequence...

    Code:
    int start_num = 0;
    int end_num = 0; 
    int increment_num = 0;
    int x = 0; //Counter
    
    // Get user input
    
    for (x = start_num; x <= end_num; x += increment_num) {
      printf("%d\n", x); // Print each number on it's own line
    }
    Of course if the user specifies an incremental value that will never count to the end number, then the end number will not be in the sequence, but the loop will stop when the bound is broken...

    if the user specifies start_num = 1, end_num = 10, and increment_num = 2 then the sequence output will be 1, 3, 5, 7, 9.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with programming assignment, please!
    By xMEGANx in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2008, 05:29 PM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 03:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

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