C++ :: Print Numbers At Random From A Set Using Single Statement

Jan 21, 2012

I have an exercise that asks me to print numbers at random from the following set (using only a single statement):

2, 4, 6, 8, 10

Here's my statement:

Code:
cout << (2 + rand() % 9) << " ";

which prints numbers at random between 2 and 10, now I can use the modulus operator in an if...else statement to print only even numbers but the exercise specifically requires using only one statement, can that be done using the conditional operator? and if not then how?

View 10 Replies


ADVERTISEMENT

C# :: Infinite Loop Random Boolean Print Statement

Feb 19, 2014

I am working on a small simple program my program fills up a air plane with customers there is 2 levels 1 and 2 and it will put that person in the spot depending on being picked.

So far i made a boolean airplane with 10 spots I can enter 1 person into the airplane with no problem but then after he sits down i get a infinite loop? I assume i maybe have to use the continue statement I feel like there is something simple that im missing i want to add more people. here is what i have

class Program
{
static void Main(string[] args)
{ bool[] planeSeats = {false, false, false, false, false, false, false, false, false, false };

[Code]......

View 1 Replies View Related

C++ :: Program Must Print Out 5 Random Numbers

Feb 10, 2014

The program must print out 5 random numbers, from 1 to 45 and 100 different sequence.. Now I want each number of sequence to be different and not the same....

for example

1,2,3,4,5
6,7,8,9,10
....
...
..

here is my code:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main () {
int xRan1;

[Code] ....

View 2 Replies View Related

C :: Loop Statement To Print Numbers From 1 To 10 In Reverse Order Separated By Star

Apr 21, 2013

Write a for loop statement to print the numbers from 1 to 10 in reverse order separated by star :

10*9*8*7*6*5*4*3*2*1

i try to do it but it show me like this :

10*9*8*7*6*5*4*3*2*1*

how do i write to show me like the first one ?

View 10 Replies View Related

C/C++ :: Using Print Statement To Print Certain Number Of Spaces

Sep 1, 2014

is there a to use printf to print certain number of blank spaces, where the number is derived from the output of a function?

for(m=low; m<=high;m++)
{
printf("t=%2d %'f(m)-1's %3c", m, ' ',*);
}

This is suppose to output

t=-3 *
t=-2 *
t=-1
.
.
.

View 2 Replies View Related

C++ :: Print Conversion Specifiers To A Single Value Multiple

Oct 4, 2013

Is it possible to apply Print Conversion Specifiers to a single value multiple .

simply put ,

printf("%.1s%6lf
", perimeter_Circle);

I want one digit precision and 7 tab space on this item.

View 1 Replies View Related

C++ :: Make 10 Random Numbers Thus Making 10 Random Flips Of Coin?

Aug 31, 2013

I want to make 10 random numbers thus making 10 random flips of a coin. I am getting 10 tails or 10 heads!

Code: #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[])
{

[Code].....

View 4 Replies View Related

C/C++ :: Print Single Linked List Backward With Functions / Classes - Error Message Regarding Header Files

Oct 11, 2014

I'm trying to print a single linked list backward with functions/classes. I have created the main file and 3 header files. But I'm getting an error on one of the header files, linkedListIterator after adding #include "linkedListType.h". It says that "linkedLlistType.h" is calling itself. And when I try to run it, I get an error of "too many header files." I have tried changing the headers many times, but nothing seems to work.

.cpp file:

/*(Printing a single linked list backward) Include the functions reversePrint and recursiveReversePrint, as discussed in this chapter, in the class linkedListType. Also, write a program function to print a (single) linked list backward. (Use either the class unorderedLinkedList or the class orderedLinkedList to test your function.)*/

#include "linkedListType.h"
#include "orderedLinkedList.h"
#include "linkedListIterator.h"
#include <iostream>
using namespace std;
struct nodeType

[Code] ....

header files:
[URL] .... (error in this header file)
[URL] ....

View 9 Replies View Related

C :: Simple Search - Any Single Numbers In Array

Jul 13, 2013

I wrote a code which checks if there are any single numbers in an array.e.g.In the next array there is a single number - "3" 4 1 4 3 4 1..Here is an array which doesn't have single numbers in it: 4 1 4 3 3 1

Code:
#include <iostream>
using namespace std;
int single (int arr[], int size)
{
int couple=0;

[Code]....

First I thought to double the number of couples I'll get from my search and find some connection to the length of the array, but it hasn't worked out (signed red).

View 9 Replies View Related

C :: Break Statement In Random Number Sorting

Jul 4, 2014

The program I am stuck on is used to generate random numbers, then sort them (found in Chapter 23, p. 212). When I compile it, I get an error stating "break statement not within loop or switch. I'm finding that there is an issue with a break statement, but I don't know enough yet.

Code:
/* Will generate random numbers, then sort them.*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main() {
int ctr, inner, outer, didSwap, temp;
int nums[10];
time_t t;

[Code] ....

View 4 Replies View Related

C++ :: Print Statement Without Spaces

Apr 13, 2013

i am trying to write a program to print a statement without spaces in it.For example, if the statement is "Hello, i am Solidsnake", then it should print it as "Hello,iamsolidsnake".

View 4 Replies View Related

C :: Take Averages Of Whole Week And Output Print Statement

Jul 17, 2013

I wanted to take the averages of the whole week and output a print statement. I've tired few ways this like assigning "temp" a pointer, or in each if statement assigning temp to a new variable like a, b , c etc. Then adding them all up at the end and dividing by 7. My issue is I am unclear how to store the number typed for each if statement while the program is running, then have it compute the simple equation. The code in question is below:

Code:
#include <stdio.h>
int main(int argc, const char * argv[]) {

int i, j = 0, temp = 0;
for (i=1; i<=7; i++) {
j++;

[Code] ....

View 1 Replies View Related

C/C++ :: Using Printf Statement To Print Certain Number Of Spaces

Sep 1, 2014

is there a to use printf to print certain number of blank spaces, where the number is derived from the output of a function?

for(m=low; m<=high;m++)
{
printf("t=%2d %'f(m)-1's %3c
", m, ' ',*);
}

this is suppose to output

t=-3 *
t=-2 *
t=-1
.
.
.

This is supposed to output t=m and then m is plugged into a function say for -3, plugging it into function f gives 23, well i want 22 blank spaces and then a asterik and go to next line. that is why i put -1 after %'f(m)-1's i

The asterisk appear right next to the t value here but they are way to the right of it when i made this post.

View 2 Replies View Related

C++ :: For Loop - Code Puts Out Switch Statement 5 Times With Random Number

Jan 26, 2014

I am writing for loop with a switch so that scores can be inputted in by a judge. The issue that I am running into is that I will put out an the text then the test happens and the code puts out the switch statement 5 times with random number. Here is what I have written.

Code:
int main() {
int diver;
int option;
int Judge;
cout << "Enter Divers Name:";

[Code] ....

View 4 Replies View Related

C :: Won't Print Numbers Sorted When Use Print Function

Nov 17, 2013

I am making a program where the user enters numbers into an array and then a number,x. The array is sorted, then x is inserted into the appropriate place. I wrote my selection sort

Code:

void Sort(int ary[], int size)
{
int temp;
int smallest;
int current;
int move;
}

[code]....

put it wont print the numbers sorted when I use my print function, just the unsorted numbers.

View 1 Replies View Related

C++ :: Program To Compute And Print A Billing Statement For Each Patient

Oct 17, 2013

Community Hospital needs a program to compute and print a billing statement for each patient. Charges for each day are as follows:

a. room charges: private (P) room = $125.00; semi-private (S) room = $95.00; or ward (W) = $75.00
b. telephone charge = $1.75
c. television charge = $3.50

Write a program to get data from the keyboard, compute the patient’s bill, and print an appropriate statement. Typical input (nut yours do not have to be identical to this) is the following:

How many days was the room occupied? 5
What type of room? P
Telephone used during the stay? N
Television used during the stay? Y

Keep in mind that the user needs to know that the input can be any integer number of days for the length of stay, either (P, S, or W) for the room type, and either (Y or N) for both telephone and television options.

A statement (which yours MUST be identical to) for the data given follows:

Community Hospital Patient Billing Statement

Number of days in hospital: 5
Type of room: Private
Room charge:$ 625.00
Telephone charge:$ 0.00
Television charge:$ 17.50

TOTAL DUE = $642.50

View 3 Replies View Related

C/C++ :: Program To Print Product Of Even Numbers And Sum Of Odd Numbers Between 1 And 30

May 20, 2014

I want to make a program to print the product of even numbers between 1 and 30 and sum of odd numbers between 1 and 30. But the answer of product is negative. The photo shows the output of the code.

#include <stdio.h>
#include <conio.h>
void main ()
{
int i, even_product=1, odd_sum=0;
for(i=1;i<=30;i++) // For loop starts here!

[Code]...

View 5 Replies View Related

C++ :: Unable To Create Random Array Of 100 Int Between 0 And 250 To Sort Them And Print

Nov 25, 2013

I seem to get an error after int main (void) saying 'a function definition isnt aloowed here before { token? And then also at the end of main saying 'expexted } at end of output?

My programme is trying to create a random array of 100 ints between 0 and 250, sort them and print them.

Here is my code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>
void bubbleSort(int *array,int length)//Bubble sort function {
int i,j;
for(i=0;i<10;i++)

[Code]....

View 4 Replies View Related

C++ :: Create Random Matrix And Print It Out In Text File

Feb 11, 2014

I am trying to create a random 3x3 matrix and print it out in a text file using basic functions. My code below will not compile.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//nxn Matrix = 3x3
const int ROWS = 3;
const int COLS = 3;

[Code] .....

View 2 Replies View Related

C/C++ :: How To Print Random Number Asterisks On Two Separate Lines

Apr 20, 2015

Im trying to figure out how to print a random number of asterisks on two separate lines at the same time. So every time you press a key it prints a different amount of random number of integers between1 and 10 until one of the lines reaches 70. I have the code to do one line but can't figure out how to do two at once.

#include <stdio.h>
#include <time.h>
#define MINR 1
#define MAXR 70
#define MINM 1
#define MAXM 10
int main (void)

[Code]...

View 2 Replies View Related

C++ :: If Else Statement To Check Multiple Model Numbers

May 2, 2013

I need to create a if else statement to check multiple model numbers (200+)... How can i build this? I basically want the user to enter their model number and if its on the list they get a approved or denied message.

So far I have this, the "if(model == 9002||1002)" doesnt seem right, it needs to basically check 200+ model numbers... am I even on the right track?

Code:
#include <iostream>
using namespace std;
int main(){
int model;
cout << "Enter model" << endl;

[Code] .....

View 3 Replies View Related

C++ :: Random Integers (numbers)

Oct 17, 2014

I'm trying to make a C++ program that generate 6 random numbers ( from 100,000 to 999,999 ), okay, so I wrote a few lines..

Code:
srand(time(0));
for (int i = 0; i < 5; i++)
{
std::cout << 100000 + (rand() % 999999) << std::endl;
}

The problem is, that it generates numbers like this:

117,207
123,303
131,083
... etc etc..

They're all starts with 100K, i want them to be an actual random..

View 8 Replies View Related

C++ :: Random Numbers Are Not In Specified Range

Nov 9, 2014

My program behaves weird... I wanted to generate 10 random numbers from 1 to 100 each of them bigger than previous, using the while loop and function that returns a random number in specified range.

When I run the program, I get numbers much bigger than 100, even negative number, and numbers are same every time I run the program.

Code:
#include <ctime>#include <cstdlib>
#include <iostream>
using namespace std;
int range(int low, int high);

[Code] .....

View 2 Replies View Related

C :: Generating Random Numbers

Oct 19, 2014

I have a program that generates random numbers. After the random number is generated, the program asks if you want to generate another random number. However, if you generate another random number, it is always the same as the first random number. How can I fix this?

View 5 Replies View Related

C :: Generate Random Numbers

Mar 17, 2013

I'm trying to generate random numbers so that I can assign people to teams. So far I have come up with this

Code:

int generateTeam(){
int i, teamNumber, c, n;
for (c = 0; c <= 5; c++) {
n = rand()%100 + 1;
}

[code]....

}//end generateTeam I'm not sure how to make it so that I can exclude the previous random number when generating the next one. As an example, I have 22 students and I get the number 19. Now I can't have 19 again because that guy already has it.

View 3 Replies View Related

C++ :: Arrays Getting Random Numbers

Oct 30, 2013

Ok so Im suppose to make this program were the user inputs the size of the array then the user sets a certain range min and max for random numbers to be generated. I have a function named fillarray()

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int fillarray();

[Code] ....

Whenever i run the program i keep on getting a loop for the size of the array.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved