I have to display this number pattern:
-3 1 5 9
7 12 17
15 21
using:
int number = __________________; // assign correct initial value
int row, col;
for (row = 1; row <= 3; row++)
{
// adjust number so it displays the correct first value in the row
________________________________________________
for (col = 1; col <= ___5 - row___; col++)
{
cout << setw (5) << number;
// adjust number so it displays the next correct value in this row
______________________number + row + 3;_________________________________
} / /end inner for loop
cout << endl;
} // end outer for loop
I know number + row + 3 gets the correct numbers across but I cannot seem to get the correct start value.
Try this
number = -3
for(row = 1; row <= 3; row++){
for (col = 0; col < 5 - row; col ++){
display (number + col * (3 + row));
}
number += 12 - row * 2;
}
With your restrictions, that would be:
int number = 0; // assign correct initial value
int row, col;
for (row = 1; row <= 3; row++)
{
// adjust number so it displays the correct first value in the row
number += 12 - (row-1) * 2 - (row+2) * (6 - row);
for (col = 1; col <= 5 - row; col++)
{
cout << setw (5) << number;
// adjust number so it displays the next correct value in this row
number = number + row + 3;
} / /end inner for loop
cout << endl;
} // end outer for loop
Explanation:
(not a bullet) + [12 - (row-1)*2] is for moving between -3, 7 and 15
(not a bullet) - [ ((row-1) + 2) * (5 - (row - 1))] is for compensating growth of the number in the internal loop
Since the result of the right part with row = 1 is -3, initial number is chosen as 0.
int number = -1; // assign correct initial value
int row, col;
for (row = 1; row <= 3; row++) {
// adjust number so it displays the correct first value in the row
number -=2;
for (col = 1; col <= ___5 - row___; col++){
cout << setw (5) << number;
// adjust number so it displays the next correct value in this row
______________________number + row + 3;
} / /end inner for loop
cout << endl;
} // end outer for loop
#include<iostream>
using namespace std;
int main()
{
int i,j;
int temp=-3;
for(i=0;i<3;++i)
{
for(j=0;j<4-i;++j)
{
cout<<temp<<" ";
temp=temp+4+i;
}
temp=temp-(4+i);
temp=temp-2;
cout<<"\n";
}
char dd;
cin>>dd;
return 2;
}
Related
Here's the situation:
User can choose up to 4 dices on the table with a range[1-12] faces they want (Yes, 1 face dice is a THING here). Then the program will calculate all the outcome possibilities.
For example: 2 dices, 1st with 6 faces, 2nd with 2 faces.
Output:
Sum of 2 = 1
Sum of 3 = 2
Sum of 4 = 2
Sum of 5 = 2
Sum of 6 = 2
Sum of 7 = 2
Sum of 8 = 1
I have found out the pattern to calculate with all of the possibilities with different of no. of dices and faces they have, here's the illustration:
4 dices with faces [6, 2, 3, 4] respectively
Click here to check the pattern
Blue area is a dice with 6 faces
Green area is 6 faces with 2 times
Yellow area is green area loop with 3 times
Read area is yellow area loop with 4 times
The numbers aside are the count of the appearance of each sum and it's correct all the time no matter what inputs are.
Every new area will be start at id[5] and after every iteration the process area will shift [i+1] space untill the end.
However, I've attempted many times and still cannot find the correct way to implement this pattern into c++ program.
My codes are like this:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int num, face, faceCounter;
int faces[15] = { 0 }, counter1[150] = { 0 }, tempCount[150] = { 0 };
cout << "How many dices: ";
cin >> num;
for (int i = 1; i <= num; i++) {
cout << "Face for dice number: " << i;
cin >> face;
faces[i] = face; //Store number of face into each dice
faceCounter += face; // Store how many outputs needed
}
// Calculate the first iteration of dices
for (int k = 0; k < faces[2]; k++) {
for (int j = num + k; j < (num + faces[1] + k); j++) {
tempCount[j]++;
}
}
// Copy results into counter1[]
for (int i = num; i <= faceCounter; i++) {
counter1[i] += tempCount[i];
}
// Find out the remaining dices
for (int i = 2; i < num; i++) {
for (int k = 0; k < faces[i+1]; k++) { // Calculate the count range
for (int j = num + k + 1; j < (num + faces[i] + k); j++) { // Add the previous counter's value into temp counter
tempCount[j] += counter1[i];
}
if (k == faces[i + 1] - 1) // Make sure finished the for loop first then to final addition, won't duplicate data
finished = 1;
}
//Load results back to counter, which will using it back in loop for further counting
for (int i = num; (i <= faceCounter) && (finished = 1); i++) {
counter1[i] += tempCount[i];
}
finished = 0;
}
for (int i = num; i <= faceCounter; i++) {
cout << "Sum of " << i << " = " << counter1[i] << endl;
return 0;
}
This is not working though.
My desired output is:
Sum of 4 = 1
Sum of 5 = 4
Sum of 6 = 9
Sum of 7 = 15
Sum of 8 = 20
Sum of 9 = 23
Sum of 10 = 23
Sum of 11 = 20
Sum of 12 = 15
Sum of 13 = 9
Sum of 14 = 4
Sum of 15 = 1
How can I change it?
I have a pascal's triangle with max rows of 5 . Lets suppose I want to find the integration of the fourth row . How do I access the fourth row in the pascal's triangle. More precisely I want to know how to access a row in the pascal's triangle by entering the number n of the row
Code
#include <iostream>
using namespace std;
int main(){
int a1, a2, a3, a4, a5, pascal, columns;
const int rows = 5;
int **array = new int *[rows]; //generating array
for(int i = 0; i <= rows; i++)
array[i] = new int [columns];
for (int i = 0; i <= rows; i++){ //loop for te pascal's triangle
for (int j = 0; j <= i; j++){
if(j == 0 || i == 0){
pascal = 1; //first element of pascal's triangle
}
else{
pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
}
cout << " " << pascal; // printing pascals triangle
}
cout << "\n";
}
cout << "enter which row to integrate: ";
// here I want to directly access a row rather that entering the elements of the row
cin >> a1;
cin >> a2;
cin >> a3;
cin >> a4;
cin >> a5;
}
1
1 1
1 2 1
1 3 3 1 ------> like of n = 4 i want to integrate the elements of this row
1 4 6 4 1
And the answer should be for 1,3,3,1 = 0, 1, 1.5, 1, 0.25
you should first fill the array with the elements then you can access them like so (EDIT: Make sure to initialize the columns variable, I set it to 5)
#include <iostream>
using namespace std;
int main() {
int row_nb, pascal, columns = 5; //Initialized columns with columns = 5
const int rows = 5;
int **array = new int *[rows]; //generating array
for (int i = 0; i <= rows; i++)
array[i] = new int[columns];
for (int i = 0; i <= rows; i++) { //loop for te pascal's triangle
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0) {
pascal = 1; //first element of pascal's triangle
}
else {
pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
}
array[i][j] = pascal; //fill the array
cout << " " << pascal; // printing pascals triangle
}
cout << "\n";
}
cout << "enter which row to intergrate: ";
// here I want to directly access a row rather that entering the elements of the row
cin >> row_nb; //input the row you want to access
for (int i = 0; i <= row_nb; i++) { //access the elements in this row in the array
cout << array[row_nb][i] << " ";
}
return 0; // add the return statement since the return type of the main function is int
}
As I said when you last asked this :
You could simply store each row into a std::vector<int> as you calculate it (in addition to, or instead of, printing it), then keep a list of rows in a std::vector<std::vector<int>>. Then, to access the nth row after calculating the triangle, you just grab the nth element of the second vector.
I need help, I created a short little program a while ago where it would print a simple pyramid with "*" like this:
*
***
*****
but I decided to challenge myself and see if I could create a simple diamond shape like this:
*
***
*****
***
*
Here is my code so far.
I should also add that the value you input, for example 5, determines how big the diamond is.
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value; i++) {
int number = 0;
number+= 2;
//print spaces v v v
for (int x = 0; x < (value - value + i + 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (/*ATTENTION: What do I do here? Plz help*/); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
What I've been trying to do is figure out what to put inside the parenthesis where it says (//ATTENTION). I've been working for at least an hour trying to do random things, and one time it worked when I input 4, but not for 5, and it's just been very hard. This is key to building the diamond, try putting in just value and compile to see what happens. I need it to be symmetrical.
I need to know what to put inside the parenthesis please. I'm sorry this is very long but the help would be appreciated thanks.
I also apologize if my code is messy and hard to read.
int number = 0; and number+= 2;
value - value inside for (int x = 0; x < (value - value + i + 1); x++) {
are not required.
Inside the parenthesis, you can use
2*(value-i-1)-1
However, I would suggest you to first analyze the problem and then try to solve it instead of trying random things. For instance, let's consider the cases of even and odd inputs i.e., 2 and 3.
Even Case (2)
*
***
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 1 1
1 0 3
2 0 3
3 1 1
For row index < value
Number of Spaces = value - row index - 1
Number of Stars = 2 * row index + 1
For row index >=value
The number of spaces and stars are simply reversed. In the odd cases, the situation is similar too with a small exception.
Odd Case (3)
*
***
*****
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 2 1
1 1 3
2 0 5
3 1 3
4 2 1
The small exception is that while reversing, we have to ignore the row index = value.
Now, if we put the above analysis in code we get the solution
//Define the Print Function
void PrintDiamond(int rowIndex, int value)
{
//print spaces v v v
for (int x = 0; x < value - rowIndex -1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < 2 * rowIndex + 1; y++) {
cout << "*";
}
cout << endl;
}
And then inside main
//Row index < value
for (int i = 0; i < value; i++) {
PrintDiamond(i,value);
}
//For row index >= value reversing the above case
//value-(value%2)-1 subtracts 1 for even and 2 for odd cases
//ignore the row index = value in odd cases
for (int i = value-(value%2)-1; i >=0; i--) {
PrintDiamond(i,value);
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value-1; i++) {
// int number = 0;
// number+= 2;
// //print spaces v v v
for (int x = 0; x < i+1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2*(value-1-i)-1); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
I hope that you will get this .Also in the second for loop you were iterating it one extra time by iterating the loop upto value. But since the pyramid is symmetric so the no of rows in the pyramid will be 2*value-1.So I in the second loop i should vary upto value -1.
This code should resolve the problem:
#include <sstream>
using namespace std;
void printSpaces(int howMany) {
for(int i = 0; i < howMany; i++) cout << " ";
}
void figure(int size) {
bool oddSize = size % 2 == 1;
int center = size / 2;
int spaces = size / 2;
// If figure is of an odd size adjust center
if (oddSize) {
center++;
} else { // Else if figure is of even size adjust spaces
spaces--;
}
for (int i = 1; i <= center; i++) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++) cout << "*";
cout << endl;
spaces--;
}
spaces = oddSize ? 1 : 0; // If the figure's size is odd number adjust spaces to 1
center -= oddSize ? 1 : 0; // Adjust center if it's an odd size figure
for(int i = center; i >= 1; i--) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++)
cout << "*";
cout << endl;
spaces++;
}
}
int main() {
int value = 0;
while(value < 3) {
cout << "Please enter in a value (>= 3): ";
cin >> value;
cout << endl;
}
figure(value);
return 0;
}
Its my first year in computer science and I'm having some trouble with this problem.
The instructor asks to write a code for a parallelogram:
Enter number of rows: 13
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
...with forced odd entry (like 4 changing to 5). rules are - I can't use stew - must draw shape using only 3 loops - plus one loop for forced entry (while r is between 3 and 23) - must use the total number of rows or current row for all calculations (can't use previous row and can't generate your own numbers)
int main() {
int control = 0;
int i = 0, j = 0, k = 0, l = 0;
int r = 0, c = 0, crntRow = 0, crntRow2 = 0,
cuur_8r = 0, space = 0, star = 0;
char a = '-', b = '+';
//cin >> r;
r = 11;
if (!(r % 2))
r++;
c = 0;
//cout << c << r;
for (i = 0; i < r; i++)
{
space = r / 2;
star = r / 2;
crntRow = i;
while (crntRow > space)
{
space++;
cout << a;
}
//cout << j;
for (int j = 0; j < c; j++)
{
if (star > j)
{
cout << b;
}
}
c++;
cout << '\n';
}
}
TLDR: this is the terrible code I came up with so far, I don't know how to shrink the number of stars after rows/2
The three loops that your instructor was referring to are:
An outer loop over the lines
A loop to prefix spaces to each line (0 spaces for the first half)
A loop to print stars on each line (this is always non-zero)
Here is a very stripped-down example:
int i, j, k, sp, st;
int r = 11;
// 1. An outer loop over the lines
for (i = 0; i < r; i++)
{
if(i <= r/2) {
sp = 0; // No spaces in the first half
st = i + 1; // Increasing stars in the first half
} else {
sp = i - r / 2; // Increasing spaces in the second half
st = r - i; // Decreasing stars in the second half
}
// 2. A loop to prefix spaces to each line (0 spaces for the first half)
for(j = 0; j < sp; j++) cout << ' ';
// 3. A loop to print stars on each line (this is always non-zero)
for(k = 0; k < st; k++) cout << '*';
cout << '\n';
}
As an excercise, you can do the same thing in two loops:
An outer loop over the lines
An inner loop over the character in each line
In this case, you would have to select which character to print during each iteration of the inner loop.
I am stuck on the last few problems of an array exercise. Could anyone lend a hand?
Write C++ statements that do the following:
store 5 in the first column of an array and make sure that the value in each subsequent column is twice the value in the previous column.
print the array one row per line.
print the array one column per line.
I think this will work for question #2:
for (row = 0; row < 10; row++)
{
for (col = 0; col < 20; col++)
cout << alpha[row][col] << " ";
cout << endl;
}
but question 1 and 3 have me stumped. thanks
Here's what i came up with after your tips. thanks everyone
3.
for (col = 0; col < 20; ++col)
{
for (row = 0; row < 20; ++row)
cout << alpha[row][col] << " ";
cout << endl;
}
1.
for (row = 0; row < 10; row++)
alpha[row][0] = 5;
for (col = 1; col < 20; col++)
for (row = 0; row < 10; row++)
alpha[row][col]=alpha[row][col-1]*2;
For #1, run a loop that starts at zero and goes until the number of rows. In each iteration just assign 5 to array[row][0]=5 (since coloumn 0 is the first coloumn).
Now run a loop from 1 to the number of coloumns. Inside, run another loop for each row. just assign array[row][col]=array[row][col-1]*2.
For #3, simply reverse the order of the loops. We iterate over all coloumns, and for each coloumns we have to iterate over all rows and print a newline after that.
I would post code, but it is better for you to try to understand and write the code yourself.
for each row, insert 5 into the first column (index 0), then in a loop, iterate from 1 through the number required, and the value at the current column index = 2 * value at previous column index (i.e. col - 1).
re-arrange the row, col loops.
Well for 1, just take the previous col and multiple by 5. So when your going through a loop, it'd be like col[position your at now] = col[prev pos]*2
For question #3, just reverse the order of loops, as
for (col = 0; col < 20; col++)
{
for (row = 0; row < 10; row++)
cout << alpha[row][col] << " ";
cout << endl;
}
Isn't it simple?
For question #1, just use the same reverse order of loops, and do this
int value = 5;
for (col = 0; col < 20; col++)
{
for (row = 0; row < 10; row++)
alpha[row][col] = value;
value = 2 * value;
}