Programming & Design
Question:
please tell me how to design in C++, which generate sequence of numbers uptil 20 entries using function?
2009-08-25 11:46:50 UTC
please tell me how to design a program in C++, which generate sequence of numbers uptil 20 entries using function??
Three answers:
Mark aka jack573
2009-08-28 10:42:47 UTC
Here is an easier program for you.
#include
using namespace std;
void sequence(int number);
int main()
{
int number; // For the sequence amount
cout << "Enter the number for the senquence: ";
cin >> number;
// You might want to check if the number is negative or over 20.
sequence(number);
}
void sequence(int number)
{
for (int i = 0; i <= 20; i = i + number)
{
cout << i << " ";
}
}
Hope that helps. You may want i to start at 1 and not 0, or even start at number.
for (int i = number; i < 20; i = i + number)
Troy Bolton(prat)
2009-08-25 18:53:29 UTC
numbers are tobe in what sequence??
I can tell u roughly-
define class
make function(void)
call ti from diferent function
for counting uptil 20 use a loop
Black Rose
2009-08-25 19:54:01 UTC
EDITED:
#include
#include
using namespace std;
const int SIZE(20);
const int MAX_RANGE(100);
// FUNCTION generates sequence of random numbers
void generate(int array_type[], const int array_size, const int max_range)
{
int index(0);
// Initialize the random generator
srand( time(NULL) );
// Generates 'array_size' numbers between 0 and MAX_RANGE
while(index!=array_size)
array_type[index++] = rand()%max_range;
}
// FUNCTION generates sequence of sequential numbers
void generate(int array_type[], const int array_size, const int start, const int step)
{
int index(0);
// assign value of 'start' to the first index
// then increment 'index' by 1
array_type[index++] = start;
// generates sequence of sequential numbers
while(index!=array_size)
array_type[index++] = start + (step*index);
}
int main()
{
// array to holds the generated numbers
int sequence[SIZE];
// call to function generate
generate(sequence, SIZE, 0, 2);
// Output the sequence
int index(0);
while(index!=SIZE)
cout<
cout<
return EXIT_SUCCESS;
}
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...