C/C++ :: Triangles With Nested For Loops?

Sep 27, 2014

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()

[Code].....

View 1 Replies


ADVERTISEMENT

C :: Nested For Loops

Jun 1, 2013

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..

View 6 Replies View Related

C :: How To Make Nested Loops

Dec 7, 2013

Howi can made nested loops?

Code:

for (yax=0; yax<10; yax=yax+1) {
for (xax=0; xax<100; xax=xax+1) {
printf("%d
",yax);
}
}

way what i tired dont work. or maybe works but why this prints only zeros ?

View 8 Replies View Related

C :: Nested Loops - Asterisk Diamond

Jul 11, 2013

Getting close but I think I am stuck on the second loop. The input you put in will be doubled (and it's not supposed to).

Code:
int main() {
int n, i, j, k;
printf("What would you like the height to be? (Positive odd integer)
");
scanf("%d", &n);

[Code] .....

View 3 Replies View Related

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 << "*";
} }

The ouput is just blank as of now.

View 2 Replies View Related

C++ :: Creating Half Triangle Using Nested Loops?

Sep 24, 2013

The output should be something like:

5
45
543
5432
54321

View 2 Replies View Related

C :: Program That Prints Multiplication Table Using Nested Loops

Mar 9, 2013

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)

View 1 Replies View Related

C++ :: Calculating Interest And Making Nested Loops Not Working

Feb 4, 2013

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:

Q| Beginning Principle| Interest Earned| End Principle
1| $1,000.00 | $13.13 | $1,013.13
2| $1,013.13 | $13.30 | $1,026.42
3| $1,026.42 | $13.47 | $1,039.89
etc

Here is the code I have so far, and I just am not quite sure where to go next.

Code:

{
cout << "Quarters" << " " << "Beginning Principles" << " " <<"Interest Earned" << " " <<"End Principal" << endl;
endprin = balance + (quarter * interest);
interest = quarter * interest;
cout << endprin << endl;

}

View 2 Replies View Related

C++ :: Nested Range For Loops With Multi-dimensional Array

Jun 1, 2014

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;

[Code] ....

View 3 Replies View Related

C :: Method Or Process For Reducing Redundant Code In Nested Loops

Nov 8, 2014

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?

View 3 Replies View Related

C++ :: Place Nested Loop Where Nested Not Necessary?

Apr 3, 2014

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.

#include <iostream>
#include <iomanip>
#include <stdio.h>

[Code]......

View 3 Replies View Related

C :: Making A Teapot With Triangles

Oct 15, 2013

i have an assignment where i have to draw a teapot using triangles, and are struggling with the part where i have to color it. Here is an link to the assignment URl....

I have managed to draw the teapot and translated it onto the screen. the part i need support with is making a bounding box for the triangle and coloring it.

View 5 Replies View Related

C++ :: Making Octagon Out Of Right Triangles

Jun 28, 2014

One exercise says that, "Define a right triangle class. Make an octagonal shape out of eight right triangles of different colors."

Making such a class isn't difficult. I wrote it as follows:

#include "Simple_window.h"
class right_triangle : public Shape {
public:
right_triangle(Point p, int l, int sh): c(p), _long(l), _short(sh)

[Code] ....
But it sounds that making an octagon using eight right triangles isn't possible!

View 2 Replies View Related

C/C++ :: ASCII Art For Different Triangles And Pyramid

Mar 15, 2015

I have to Write a program to produce that makes four triangles and a pyramid. I have to ask the user they height of the triangle and prevent the user from entering a height any larger than 25. It should look like this.

Enter the height of your triangle/pyramid: 3

***
**
*

*
**
***

*
**
***

***
**
*

*
***
*****

im having tourble with 3 and 4th and also pyramid.

HERES WHAT I HAVE SO FAR.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main () {
int input = 0;
cout << "Please enter the height of your triangle/pyramid: ";

[Code] .....

View 2 Replies View Related

C/C++ :: Making Octagon Out Of Right Triangles?

Jun 28, 2014

One exercise says that, "Define a right triangle class. Make an octagonal shape out of eight right triangles of different colors."

Making such a class isn't difficult. I wrote it as follows:

#include "Simple_window.h"
class right_triangle : public Shape {
public:
right_triangle(Point p, int l, int sh): c(p), _long(l), _short(sh) {
add(Point (p));}
void draw_lines () const

[code]...

But it sounds that making an octagon using eight right triangles isn't possible!

View 4 Replies View Related

C++ :: Place Two Asterisk Triangles On Top Of Each Other But Only Using 3 For Statements

Oct 19, 2014

I have to place two asterisk triangles on top of each other BUT only using 3 for statements. I have gotten the first one:

for(int a=1;a<=10;a++) {
for(int b=1;b<=a;b++)
cout << "*";
cout << endl;
}

I need the output to look like this:
*
**
***
****
*****
******
*******
********
*********
**********
*
**
***
****
*****
******
*******
********
*********
**********

The only kicker is I can have a total of 3 nested for loop statements.

View 7 Replies View Related

Visual C++ :: Making Octagon Out Of Right Triangles

Jun 28, 2014

One exercise says that, "Define a right triangle class. Make an octagonal shape out of eight right triangles of different colors."

Making such a class isn't difficult. I wrote it as follows:

Code:
#include "Simple_window.h"
class right_triangle : public Shape {
public:

[Code]...

But it sounds that making an octagon using eight right triangles isn't possible!

View 9 Replies View Related

C/C++ :: How To Save Triangles / Polylines Into Vector Using New Keyword

Oct 11, 2014

In C++ by FLTK, to define a circle we use some code like this:

Circle c(Point(x,y), r);

And we can using vector_ref put and save them in a vector, like:

Vector_ref<Circle> vc;
vc.push_back(new Circle(Point(x,y),r));

Ok, those were about Circle and no problem till now!

Triangle can be defined like this for using in codes:

Graph_lib::polyline poly;
poly.add(Point(x1,y1),r1);
poly.add(Point(x2,y2),r2);
poly.add(Point(x3,y3),r3);

and this is a vector to saving them:

Vector_ref<Graph_lib::Polygon> vp;

The problem is that how to save/put triangles/polylines into that vp vector using new keyword like the circle does?

My sig: Save Cobani.

View 4 Replies View Related

C/C++ :: Find Triangle In Array Of N Triangles Which Has Largest Area

Apr 6, 2015

How to find triangle in an array of n triangles which has the largest area?

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
typedef struct {
double x,y;

[Code] .....

View 14 Replies View Related

C++ :: Structured Data - Nesting Triangles Using Structs And Doing Some Geometric Calculation

Sep 8, 2013

nestedTriangles.cpp description:

The program takes as input a pair of triangles, specified be giving the coordinates of each trangle's vertices. It then determines if either triangle is "nested" within the other, meaning that one triangle lies entirely within the interior of the other.

Pseudocode:

One triangle lies within another if and only if all three vertices of the first triangle lie within the interior of the second triangle.

Suppose that we have a triangle with vertices A, B, and C, described by the coordinates (xA, yA), (xB, yB), and (xC, yC), respectively. The sides of the triangle are the line segments AB, BC, and CA.

A line passing through two points (x1, y1) and (x2, y2) can be considered to be the set of points (x,y) satisfying the equation

f(x,y) = 0
where f(x,y) is given as
f(x,y) = (x - x1) (y2 - y1) - (y - y1) (x2 - x1)

One of the interesting things about that f(x,y) is that we can use it to determine which "side" of the line an abitrary point (x,y) is on:

If f(x,y) = 0, the point is exactly on the line. All points for which f(x,y) > 0 are on one side of the line, and All points for which f(x,y) < 0 are on the other side So the problem of determining whether a point (x,y) is on the inside of a trangle can be checking the sign of f(x,y) for each of the three lines making up the triangle. A complicating factor is that we don't know, for any given triangle, whether those three signs should be all positive, all negative, or some mixture of the two.

The centroid of a triangle can be computed as the "average" of the x and y coordinates of the vertices:

xcen = (xA + xB + xC)/3
ycen = (yA + yB + yC)/3

This point (xcen, ycen) is definitely inside the trangle (unless the triangle is "degenerate" and has no interior points). The problem of determining whether (x,y) is on the inside of a triangle can therefore be resolved by checking to see if it is on the same side of each of the trangle's line segments as (xcen, ycen).

What I need:

I want to fill in the missing bodies for the functions eval and areOnSameSideOf, which manipulate line segments. I think calling eval from within areOnSameSideOf will simplify the implementation of the latter.

Code:
#include <iostream>
using namespace std;
/**
* 2D Cartesian coordinates
*/

[Code]....

View 2 Replies View Related

C++ :: Structured Data / Nesting Triangles Using Structs And Doing Some Geometric Calculation?

Sep 8, 2013

nestedTriangles.cpp description:

The program takes as input a pair of triangles, specified be giving the coordinates of each trangle's vertices. It then determines if either triangle is "nested" within the other, meaning that one triangle lies entirely within the interior of the other.

Pseudocode:

One triangle lies within another if and only if all three vertices of the first triangle lie within the interior of the second triangle.Suppose that we have a triangle with vertices A, B, and C, described by the coordinates (xA, yA), (xB, yB), and (xC, yC), respectively. The sides of the triangle are the line segments AB, BC, and CA.A line passing through two points (x1, y1) and (x2, y2) can be considered to be the set of points (x,y) satisfying the equation

f(x,y) = 0
where f(x,y) is given as
f(x,y) = (x - x1) (y2 - y1) - (y - y1) (x2 - x1)

One of the interesting things about that f(x,y) is that we can use it to determine which "side" of the line an abitrary point (x,y) is on:

If f(x,y) = 0, the point is exactly on the line.

All points for which f(x,y) > 0 are on one side of the line, and All points for which f(x,y) < 0 are on the other side So the problem of determining whether a point (x,y) is on the inside of a trangle can be checking the sign of f(x,y) for each of the three lines making up the triangle.

A complicating factor is that we don't know, for any given triangle, whether those three signs should be all positive, all negative, or some mixture of the two.

The centroid of a triangle can be computed as the "average" of the x and y coordinates of the vertices:

xcen = (xA + xB + xC)/3
ycen = (yA + yB + yC)/3

This point (xcen, ycen) is definitely inside the trangle (unless the triangle is "degenerate" and has no interior points).

The problem of determining whether (x,y) is on the inside of a triangle can therefore be resolved by checking to see if it is on the same side of each of the trangle's line segments as (xcen, ycen).

I want to fill in the missing bodies for the functions eval and areOnSameSideOf, which manipulate line segments. I think calling eval from within areOnSameSideOf will simplify the implementation of the latter.

Code:
#include <iostream>
using namespace std;
/**
* 2D Cartesian coordinates
*/
struct Point {
double x;
double y;

[code]...

View 1 Replies View Related

C/C++ :: How To Clear Nested Stl Map

May 21, 2013

here is a nested stl map

typedef struct struct_RISCP {    
        float nSCPTotal;
        int nSCPCount;
        CString strIMEI;  
        std::map<UINT8, int> mapSection;    
    }RISCPSt;
map<stRncCellIdntyDmnType, RISCPSt>m_mapRISCP;

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.

View 5 Replies View Related

C++ :: Nested Classes And Structs

Mar 12, 2014

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?

View 1 Replies View Related

C++ :: Nested Conditional Operations

Feb 4, 2014

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.

View 3 Replies View Related

C++ :: Variable Nested For Loop

Mar 25, 2013

This code is used in scientific calculation for optimization problem.

Basically a particle is moving in a three dimensional space, its position is (x,y,z).

At each position, there is a fitness value associated with that position.

The fitness value is given by fitness(x,y,z) (code line 12~19).

We need to find out, when the particle moves around randomly, what is the highest possible fitness value.

To solve this, below code is used, and it produces correct result.

#define DIMENSION 3
#define MAXX 4
#define MINX 0
#define MESHsize 1
#include <iostream>
using namespace std;
float maxValue = 0.0;

[Code] ....

The output of the code:

[ fitness(0,0,0) = 0] [ fitness(0,0,1) = 1] [ fitness(0,0,2) = 4] [ fitness(0,0,3) = 9]
[ fitness(0,1,0) = 1] [ fitness(0,1,1) = 2] [ fitness(0,1,2) = 5] [ fitness(0,1,3) = 10]
[ fitness(0,2,0) = 4] [ fitness(0,2,1) = 5] [ fitness(0,2,2) = 8] [ fitness(0,2,3) = 13]
[ fitness(0,3,0) = 9] [ fitness(0,3,1) = 10] [ fitness(0,3,2) = 13] [ fitness(0,3,3) = 18]

..... so on

Answer: highest fitness = 27

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?

( Note: possibly N = 30 )

View 19 Replies View Related

C++ :: Vector In Nested Structure

Nov 26, 2013

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;

[Code] .....

View 3 Replies View Related







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