C++ :: Inverted Trapezoid Display Using Nested For Loops
Oct 6, 2013
The user will enter the number of '*'s on the 1st row (ntop) and then the number of rows forming the trapezoid (nrows). (using <iostream>, cout)
For instance, an inverted trapezoid with 7 '*"s in the 1st row and 3 rows forming the inverted trapezoid looks like:
1*******
2 *****
3 ***
(this pyramid is centered, in case it isnt when its posted). Also, each descending row has two less asteriks than the above row.
I am having trouble with the four loop displaying the number of "*" and " ". I know its a relationship with variables in the for loops, my output is just never doing what i want it to.
THis is the guideline for the for loop:
Use for loops to display the inverted trapezoid. Your outer for loop will iterate the total number of rows times. For each row use one nested for loop to display blanks (the 1st row contains no blanks) and another nested for loop to display the characters '*'.
Heres my for loops so far:
for (i = nrows; i >= 1; i--) {
for (j = 0; j >= nrows; j++) {
cout << " ";
} for (k=ntop; k >= 2; k--) {
cout << "*";
} }
What output would you expect from this program?" The output was not what I expected. I've psuedo-coded this out and I'm still missing something.
Code:
#include <stdio.h> int main () { int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int i, j; }
[code]....
The output: Code: 1 1 2 4 8 16 32 64 128 256 So when I look at this first loop I see that j = 0, which is less than 10, so then the program statement should commence, which is another for loop. So in this inner for loop I see that i = 0, which is not less than j, so this loop should terminate. Then the value of j increments by 1 and the first go around of the loop has completed.
Now I see that j = 1, so this is less than 10, and the inner for loop commences once again. This time though, i actually is less than j, so numbers[1] = numbers[1] + numbers [0], or numbers[1] = 0 + 1. Now the value of i is incremented by 1 and the first go around of this inner loop has completed. Then the value of j increments by 1 and another go around of that loop has completed.
So now j = 2, i = 1, and numbers[2] ( which is 0 ) = numbers[2] + numbers[1], or numbers[2] = 0 + 1. I was expecting the output to be an array full of 1's. However this is not the case..
So in class our teacher assigned us a program where we have to use nested for loops to creates triangles. How does the 2nd for loop print more than 1 star? since the for loop will only run the cout 1 time until it gets to the escape sequence, how does it print more than 1 star on a line? this is what is confusing me. I feel like if i can grasp the understanding of that and what the for loops are doing i can finish the rest of this program with ease
#include<iostream> using namespace std; int main()
Write a program that prints a multiplication table using nested loops. In main ask the user for the smallest column number , the largest column number and the size of the increment. Ask the user for the same information for the row values.
In the example the column values entered are: 5, 15 and 2 and the row values 3, 6 and 1.
Given those numbers you would generate the following table.
Multiplication Table | 5 7 9 11 13 15 ___|___________________________________ | 3 | 15 21 27 33 39 45 4 | 20 28 36 44 52 60 5 | 25 35 45 55 65 75 6 | 30 42 54 66 78 90 Print the 24 values with the grey background. The other numbers show the values to be multiplied.
Code: #include<stdio.h> main() { int a,b,c,d,e,f; int i,j,total; printf("Please enter smallest column number: "); scanf("%i",&a); printf("
[Code] ....
Challenge: As an added challenge try to print out the column headings (5 7 9 11 13 15) and the row headings (3 4 5 6)
For each quarter, calculate and display the beginning principal balance, the interest earned, and the final principal balance for the quarter.For example: The user entered 1000.00 for the beginning principal balance, 5.25 for the interest rate, and 8 for the number of quarters. The output from this part of the program should be similar to the following:
This is a test program that takes a number of arguments from the command prompt and concatenates them into a string object. I was looking into the possibility of using the range-based for loop for this purpose. Can it be done with pointer based arrays? I am mainly doing this because I want to have a firm understanding of range-based for, but also would like to do this with least amount of code possible.
This is my working program:
#include <string> #include <iostream> int main(int argc, char *argv[]) { if (argc > 1) { std::string concatenatedArgs;
[Code] ....
Can I somehow replace my while-loop with a range-based for? I tried the following but the compiler points out that begin and end were not declared in the scope of the range-based for loop.
#include <string> #include <iostream> int main(int argc, char *argv[]) { if (argc > 1) { std::string concatenatedArgs;
Have an assignment due in a few weeks and I'm 99% happy with it My question is is there a method or process for reducing redundant code in nested loops. Ie my code compiles and runs as expected for a period of time and after a few goes it omits a part or prints an unexpected out ext so basically how to find when the redundancy occurs with out posting my code so I can learn for my self?
I am writing a program to display a triangular pattern using nested loops and user input for the size of the base and character used to generate the image. 99% of the program runs fine. However I am having trouble with some of my input validation.
When given an integer value for the base I can verify if it is within a certain range (0-80). However, when the user inters a "char" instead of an "int" the program enters an infinite loop.
Here is the piece of code giving me trouble. (I wont bother you with the entire chunk of code, I have commented out everything else to narrow down the problem bit)
// Pattern Displays // program that asks user for the size of the base of a triangle and then generates it in a character the user chooses
#include <iostream> #include <istream> #include <string> #include <cstdlib> using namespace std; int main () { char escape;
#include <iostream> #include <cmath> using namespace std;
//prototypes for all separate functions int chooseFunc(int); int chooseMethod(int); int numRect(int);
[Code] .....
It seems that my program will ask the user for the number of rectangles and trapezoids but will fail to compute the area under the curve from the user input. Therefore, I believe I have a logical error somewhere or I'm forgetting to call something somewhere.
I'm trying to write some code that will integrate a function using the Trapezoid Rule. Using an online calculator I know the result should be 36621130.371094 but instead my code is giving me 31232938.000000 ....
#include <stdio.h> #include <math.h> int main() { long sum1; float i, area;
Similar to Pascal’s triangle, the difference triangle has some interesting properties that find applications in various fields of the natural and applied sciences. In simple terms, a difference triangle is a set of integers arranged in an inverted triangle where each inverted triangle triad has its lower element equal to the difference (absolute value) of the two elements in the upper row. A difference triangle can be created from a sequence of integers forming the uppermost row by iteratively taking differences between consecutive terms to form the next row until a single-element row is created.
Example Consider the sequence 5, 8, 13, 21, 34, 55 from the Fibonacci series as the uppermost row of the difference triangle. The difference between successive elements form a new set: 3 (= 8 – 5), 5 (= 13 – 8), 8 (= 21 – 13), 13 (= 34 – 21), and 21 (= 55 – 34). The process can then be repeated until there is only one element left giving the following difference triangle:
5 8 13 21 34 55 3 5 8 13 21 2 3 5 8 1 2 3 1 1 0
Problem Write a program that forms a difference triangle using a given series of numbers as topmost row.
I wrote this code, and everything was working well, but part of the assignment is that it must include nested loops. Once I added the nested while loop, which is basically an if statement, my life was ruined. I am trying to nest a loop in the code that will basically tell the compiler that if the value "loopVol" were to exceed the value of "final" after adding an increment, to run the program for the "final". How can I do that?
Example:
initial = 10 final = 123 increment = 10
as of now, the program would stop at 120, but I want to nest a loop that will tell the compiler to calculate at the final if this happens.
I created class called students which suppose to store students names of in array but when I call the display function it display only the first name. but I want it to display names depending on the array size.
#include <iostream> #include <sstream> using namespace std; const int SIZE=5;
I know this is more simple than I am making it out to be. I have a solar panel hooked up to an 8-Bit ADC and linked into my Microprocessor. I am trying to take the Binary readout from the ADC and convert it to a decimal number to be displayed on the boards LCD display. I am wiring the ADC to PORTA on my Motorola Freescale Board. This is for a final project for my electronic systems class due on Monday...
it occupied too much memory,i wanted to clear them(both outer map and inner map) ,some one told me you just need to call m_mapRISCP.clear(),then the mapSection (inner map) will be cleared automaticly,in other words, m_mapRISCP.clear() will clear both outer map and inner map.
I'm working on a project involving nested classes and structs like this:
Code: class A { public:class B { public:f() {A::C* iCanDoThis; //no errors. iCanAlsoDoThis->root->.... //this also works fine.}private:A::C* iCannotDoThis //this is what I would like to do. Has errors A* iCanAlsoDoThis;};private:struct C {..data..};
C* root;};
Is it possible make a pointer to struct C a private member of class B?
So I think I am having syntactical problem with my code.
Code: int main() { vector<int> ivec; int score; [Code] ....
I get an error from my compiler on the ?10th? line (Nested condition line) that says |19|error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'|
The purpose of the program is to take input and store it in a vector and then change the value to be between 1-6. I made this for the purpose of learning about nested conditional operations.
Note: In this case, the values of x,y and z is integers from 0 to 3 inclusive.
For 3-dimensional space above, actually the code had run through 3 nested "for" loops.
Question: Above code works for 3-dimensional space. How to generalize the code, so that it works also for N-dimensional space, where N is an arbitrary integer?
I have a nested record structure in C++ that contains a FileHeader record, a RecordHeader record and a DataRecord record. The last item is an array of unknown size (at compile time). I have to open a file and read the array size and then create the array.
I have worked on this for some time and can't figure out how to make it work. I know the sample data I am using has 85 records. I insert 85 into the array size and it works fine. But how do I create a dynamic array or define a vector within a nested structure?
1. What is the best (easiest) method to accomplish this (array or vector)? 2. How would it be implemented/how do you add data to it?
PreviousLogRecord.FaultRecord.push_back(field1); // does not work PreviousLogRecord.FaultRecord[recordNumber].field1 = somedata; // works with 85 in array size. struct LogFileHeaderRecordType { QString field1; int field2;
I'm trying to output a pattern using loops. The pattern is a plus symbol made of 3 rows of 5 x's followed by 3 rows of 15 x's and finally 3 rows of 5 x's.
I can get the program to output all 9 rows with 5 x's but I don't know how to do the 3 rows of 15 in the middle. I have tried a while loop and also an if statement but nothing seems to work.