I want make a star pattern just like this.
*
* * *
* * * * *
* * *
*
I am using this code.
for (int i=1; i<=3; i++) {
for (int j=1; j<=3-i; j++) {
printf(" ");
}
for (int k=1; k<=2*i-1; k++) {
printf("x");
}
printf("\n");
}
This will give
*
* * *
* * * * *
How I will iterate in decrement order.
when I am using
for (int i=3; i>=0; i--) {
for (int j=1; j<=3-i; j++) {
printf(" ");
}
for (int k=1; k<=2*i-1; k++) {
printf("x");
}
printf("\n");
}
xxxxx
xxx
x
How we merged it?
Another logic here
int x=11;
int y=x/2;
int z=1;
BOOL b1=true;
BOOL b2= true;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
printf(" ");
}
for(int k=0;k<z;k++)
{
printf("*");
}
if(y==0) b1=false;
if(z==x) b2=false;
y=b1?y-1:y+1;
z=b2?z+2:z-2;
printf("\n");
}
Try with below logic,
for (int i=1; i<=3; i++) {
for (int j=1; j<=3-i; j++) {
printf(" ");
}
for (int k=1; k<=2*i-1; k++) {
printf("*");
}
printf("\n");
}
for (int i =2; i>=0; i--) {
for (int j=1; j<=3-i; j++) {
printf(" ");
}
for (int k=1; k<=2*i-1; k++) {
printf("*");
}
printf("\n");
}
Output Will look like :
*
***
*****
***
*
The given pattern is a combination of simple pyramid star pattern and inverted pyramid star pattern. It consist of N*2-1 rows (for this case N=3). Each row contain spaces and stars in printed in increasing and decreasing order.
Stars are printed in increasing order till Nth row. After Nth row stars are printed in decreasing order.
Spaces are printed in decreasing order till Nth row. After Nth row spaces are printed in increasing order. Point your mouse cursor over the pattern to count total spaces.
Step by step descriptive logic to print diamond star pattern.
Input number of rows to print from user (in real number of rows/2). Store it in a variable say rows.
Declare two variables to keep track of total columns to print each row, say stars=1 and spaces=N-1.
To iterate through rows, run an outer loop from 1 to rows*2-1. The loop structure should look like for(i=1; i<rows*2; i++).
To print spaces, run an inner loop from 1 to spaces. The loop structure should look like for(j=1; j<=spaces; j++). Inside this loop print single space.
To print stars, run another inner loop from 1 to stars*2-1. The loop structure should look like for(j=1; j<=stars; j++). Inside this loop print star.
After printing all columns of a row, move to next line i.e. print new line.
Check if(i < rows) then increment stars and decrement spaces. Otherwise increment spaces and decrement stars.
Try Below code:-
int i, j, rows;
int stars, spaces;
printf("Enter rows to print : ");
scanf("%d", &rows);
stars = 1;
spaces = rows - 1;
/* Iterate through rows */
for(i=1; i<rows*2; i++)
{
/* Print spaces */
for(j=1; j<=spaces; j++)
printf(" ");
/* Print stars */
for(j=1; j<stars*2; j++)
printf("*");
/* Move to next line */
printf("\n");
if(i<rows)
{
spaces--;
stars++;
}
else
{
spaces++;
stars--;
}
}
Related
I wanted to ask for a information.
I am studying ROOT.
I have 100 files and each file has 9 columns.
I need to take the x data of the first column and for every other y the relative values.
using the push_back method i create the vectors X and Y[8].
Now to normalize it, I need to take the smallest value among the elements of X and translate all the points and make a graph with all the points translated.
I wanted to ask where I am going wrong.
M= 8 columns
I = 100 file with 9 columns-- 1 for X and 8 for Y — for (int i=0; i<N; i++){ etc…
int npoints = (int)X.size();
for (int k=0; k<M; k++) {
g[i][k]= new TGraph();
g[i][k]->SetNameTitle( Form("graphic_name_%d_%d",i,k), Form("graphic_name_ %d_%d",i,k) );
}
for (int p=0; p<M; p++) {
for( int b=0; b<npoints; b++){
float a=X[0];
double t;
t=b-a;
g[i][p]->SetPoint(b,X[t],Y[p][t]);
}
}
please,can you help me?
thanks
This code snippet might give you some ideas on how to approach your problem:
#define NFILES 100
#define NROWS 20
#define NCOLUMNS 9
void graphs()
{
// Create some fake data
float data[NFILES][NROWS][NCOLUMNS];
for (int k=0; k<NFILES; k++) {
for (int i=0; i<NROWS; i++) {
for (int j=0; j<NCOLUMNS; j++) {
data[k][i][j] = k*10+i*2.5+j*0.01;
}
}
}
// Allocate graphs
TGraph* graphs[NFILES][NCOLUMNS-1];
for (int k=0; k<NFILES; k++) {
for (int j=0; j<NCOLUMNS-1; j++) {
graphs[k][j]= new TGraph();
graphs[k][j]->SetNameTitle( Form("graphic_name_%d_%d",k,j), Form("graphic_name_ %d_%d",k,j) );
}
}
// Fill graphs
for (int k=0; k<NFILES; k++) {
const float a = data[k][0][0];
for (int j=0; j<NCOLUMNS-1; j++) {
for( int i=0; i<NROWS; i++) {
graphs[k][j]->SetPoint(i,data[k][i][0]-a,data[k][i][j+1]);
}
}
}
}
Basically the code here takes some string and gives a string wihtout special character .
But when I give input, the Output gives the desired output. But it gives me infinite output.
What I want is user will give infinite input and will get desired output .
int main()
{
int g= 2; char C;
while (1) {
char s[257];
scanf("%[^\n]", s);
int i;
int j= strlen (s);
for (i=0; i< j; i++){
if( (s[i]>='a'&& s[i]<='z')|| (s[i]>='A' && s[i]<='Z') || (s[i]==' ') )
printf ("%c",s[i]);
}
}
return 0;
}
I need to perform an accumulative sum in an array in Java according to iteration number, like this.
for (int it = 1; it < 3; it++) {
for(int i = 0; i < simSource.length; i++) {
for (int j = 0; j < resulting.length; j++) {
results [i][j] = resulting[i][j] + (simSource[i] * 0.5)/3;
}
}
newResults = results;
}
Thus, in each iteration, the values in the array results[i][j] increase, and these values, are stored in an array. This process is repeated until the max amount of iterations - in the first "for".
My question/trouble is: How to store in the array the final values after each iteration?
Thank you very much.
I am doing a year of college and am having MUCH difficulty at a specific point ....
I need to make a code that reads the population of the U.S. and Brazil, knowing that the population of the U.S. is higher than in Brazil.
The issue is I need to count the number of the while loop, after all if 1 means 1 more year if it is 2 years older is 2 and so on. the code is here and is objective-c...
int pAmericana, pBrasileira, ano;
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSLog(#"What year is it?");
scanf("%i", &Ano);
NSLog(#"which the American people?");
scanf("%i", &pAmericana);
NSLog(#"which the Brazilian people?");
scanf("%i", &pBrasileira);
while (pAmericana >= pBrasileira) {
pAmericana=(pAmericana*0.2)+pAmericana;
pBrasileira=(pBrasileira*0.4)+pBrasileira;
}
//FAILURE TO KNOW HOW TO COUNT HOW OFTEN HAPPENS THE LOOP .....
}
return 0;
}
You can use a counter inside the loop:
int i = 0;
while (pAmericana >= pBrasileira) {
pAmericana=(pAmericana*0.2)+pAmericana;
pBrasileira=(pBrasileira*0.4)+pBrasileira;
i++;
}
NSLog(#"Loop count %i", i);
Adding a count variable to the while loop works. But you can also use a for loop:
int i = 0;
for (; pAmericana >= pBrasileira; i++) {
pAmericana=(pAmericana*0.2)+pAmericana;
pBrasileira=(pBrasileira*0.4)+pBrasileira;
}
NSLog(#"Loop executed %d times", i);
You must declare the variable before the for loop so its value is available after the loop.
Note - you can change the calculations to:
int i = 0;
for (; pAmericana >= pBrasileira; i++) {
pAmericana *= 1.2;
pBrasileira *= 1.4;
}
int i = 0 before the while, i++; in the while loop, NSLog(#"Loops: %d", i); after the loop.
You can declare an integer variable outside the while loop then increment it within the loop, like this:
int i = 0; // Initialise the counter
while (pAmericana >= pBrasileira) {
...
i++; // Increment the counter
}
I am trying to print out arrays incrementally like this;
TractMultBox->Text = rows[0] + newline;
TractMultBox->Text += rows[1] + rows[0] + newline;
TractMultBox->Text += rows[2] + rows[1] + rows[0] + newline;
which would give an output like this
3
43
543
I can do fine with this code, however. It would like to use a for loop, that would make it easier, since I would like it to output all arrays until max is reached automatically.
I'm assuming you want to concatenate and not sum.
string text;
for (int i = 0; i < rows.count; ++i)
{
text = rows[i] + text;
TractMultBox->Text = text + newline;
}
for less lines of code.
string text = newline;
for (int i = 0; i < rows.count; ++i)
{
TractMultBox->Text = (text = rows[i] + text);
}
but that's a little hard to read.
Sounds like a job for a for loop indeed perhaps something like this:
#include <iostream>
int main()
{
int rows[3] = {3, 4, 5};
for (int i(0); i < 3; ++i)
{
for (int j(i); j >= 0; --j)
std::cout << rows[j];
std::cout << "\n";
}
std::cin.get();
return 0;
}
If rows contained 345 this would give you the following output:
3
43
543
Not sure if that's what you wanted but you can adjust the loops accordingly. The key is to have 2 for loops.
Edit: Changed to self contained example you can play with
What about a double loop like:
for (int i = 0; i < maxNRows; ++i)
{
for (int j = 0; j < i; ++j)
{
TractMultBox->Text += rows[j];
}
TractMultBox->Text += newline;
}