C++, Combing programs into one

  • C/C++
  • Thread starter zarentina
  • Start date
  • Tags
    Programs
In summary, the conversation discusses combining four programs that produce right triangles into one program using nested for loops. The desired output is four right triangles printed on the same line, separated by a tab. The code provided includes the first two programs, which are the initial and second triangle, and the goal is to combine them into one program. The code provided by the expert includes a matrix size that can be changed to adjust the output size, and uses a nested for loop to print the four triangles on one line with a tab in between.
  • #1
zarentina
12
0
Hello all, I am still new to C++ and was instructed to write four programs all a bit different and eventually combine them into one using a series of nested for loops to get an out put that resembles but goes to 10 lines of output not 5: *NOTE* all these will be separated by a tab cout<<"\t"

* ***** ***** *
** **** **** **
*** *** *** ***
**** ** ** ****
***** * * *****

**Sorry for this the spaces were not showing up between the outputs, they are essentially 4 right triangles rotated and spaced by a tab.**

I have all of the individual programs written, and I can combine the second two without any hiccups yet when I try to combine the first two I begin to run into trouble.

My code for the first two is as follows;

#include<iostream>
using namespace std;
int main ()
{
int i,j,k, range=10;
for (i=0;i<10;i++)
{
for (j=0; j<=i; j++)
{
cout<<"*";
}
cout<<endl;
}

system("pause");
return 0;
}

and the second is:

#include<iostream>
#include <iomanip>
using namespace std;
int main ()
{
int i,j,k, range=10;
for (i=0;i<10;i++)
{
for (j=10; j>i; j--)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
return 0;
}


I'm starting to think maybe my programs for the first two outputs are incorrect? For the second two I simply removed the "cout<<endl;" and replaced it with cout<<"\t"; but this doesn't seem to work for the first two.

Any advice would be greatly appreciated!
Thanks,
Z

p.s sorry for the lengthy post :/
 
Technology news on Phys.org
  • #2
It won't hurt if you will explain what the four programs should do, and what the combined program should do.

Post you sources using code tags:

[noparse]
Code:
int main()
{
  return 0;
}
[/noparse]

is displayed as

Code:
int main()
{
  return 0;
}
 
  • #3
Sorry about that, the four codes will combine which produces an output of four right triangles rotated and printed on the same lines. Such as was in my original post, yet for some reason those got pushed together and made it seem as one block of *'s not four triangles.My first program is the initial triangle.
Code:
 #include<iostream>
using namespace std;
int main ()
{
int i,j,k, range=10;
for (i=0;i<10;i++)
{
for (j=0; j<=i; j++)
{
cout<<"*";
}
cout<<endl;
}

system("pause");
return 0;
}

My second program is the second triangle
Code:
 #include<iostream>
#include <iomanip>
using namespace std;
int main ()
{
int i,j,k, range=10;
for (i=0;i<10;i++)
{
for (j=10; j>i; j--)
{
cout<<"*";
}
cout<<endl;
}
system("pause");
return 0;
}

My goal is to combine them into one program using for loops while they remain in triangle shape separated by a tab.

Thanks,
 
  • #4
Now that you're using code tags, your code would be more understandable if you indented it in a way that shows the logical structure, rather than having every line start at the left margin. I have done this with your code.
zarentina said:
Sorry about that, the four codes will combine which produces an output of four right triangles rotated and printed on the same lines. Such as was in my original post, yet for some reason those got pushed together and made it seem as one block of *'s not four triangles.


My first program is the initial triangle.
Code:
#include<iostream>
using namespace std;
int main ()
{
   int i,j,k, range=10;
   for (i=0;i<10;i++)
   {
      for (j=0; j<=i; j++)
      {
         cout<<"*";
      }
      cout<<endl;
   }

   system("pause");
   return 0;
}

My second program is the second triangle
Code:
#include<iostream>
#include <iomanip>
using namespace std;
int main ()
{
   int i,j,k, range=10;
   for (i=0;i<10;i++)
   {
      for (j=10; j>i; j--)
      {
         cout<<"*";
      }
      cout<<endl;
   }
   system("pause");
   return 0;
}

My goal is to combine them into one program using for loops while they remain in triangle shape separated by a tab.

Thanks,
 
  • #5
May be help with you.
Code:
#include <iostream>

using namespace std;

#define MATRIX_SIZE 10  //change it to any size you want to show at here

int main()
{
	int i,j;
	for (i = 0;i < MATRIX_SIZE;i++)
	{
		for (j = 0 ; j < i+1; j++)
		{
			cout<<"*";
		}

		//cout<<" ";   //I think may be this line is what you want
		cout<< "\t"; 
 
		for ( ;j <= MATRIX_SIZE ; j++)
		{
			cout<<"*";
		}
		cout<<endl;
	}
	system("pause");
	return 0;
}
 

1. What is C++?

C++ is a high-level programming language that is widely used for developing operating systems, browsers, games, and other complex software applications. It was developed by Bjarne Stroustrup in 1983 as an extension of the C programming language.

2. What does it mean to "combine programs into one" in C++?

In C++, combining programs into one refers to the process of integrating multiple source code files into a single executable program. This allows for better organization, easier maintenance, and improved performance.

3. How do you combine programs in C++?

To combine programs in C++, you need to use a build tool like make or CMake. These tools allow you to specify the different source code files that make up your program and compile them into one executable file.

4. What are the benefits of combining programs in C++?

Combining programs in C++ can improve the overall structure and readability of your code, reduce the number of files needed for your program, and potentially improve its performance. It also makes it easier to share and distribute your program as a single file.

5. Are there any potential drawbacks to combining programs in C++?

One potential drawback of combining programs in C++ is that it can make the compilation process longer and more complex. Additionally, if you make changes to one part of the combined program, you may need to recompile the entire program, which can be time-consuming.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
1
Views
993
  • Programming and Computer Science
Replies
3
Views
734
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
22
Views
2K
Back
Top