Programming Assisgnment Help

I need help im trying to get my output to read like this attachment.
https://ibb.co/Zcw3Bw5

#include <iostream>
#include <ctime>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int loc = 40, i, j, k;
srand(time(NULL));
for
(i = 0; i < 40; i++)
{
{
loc += rand() % 3 - 1;
//cout << loc;
}

for (j = 0; j <= loc; j++)
{
cout << ".";
}
cout << "*";

for (k = loc; k < 78; k++)
{
cout << ".";
}

cout << endl;


}
return 0;
} // main
Works for me - what's the problem?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
  int loc = 40, i, j, k;
  srand(time(NULL));
  for (i = 0; i < 40; i++) {
    {
      loc += rand() % 3 - 1;
    }
    for (j = 0; j <= loc; j++) {
      cout << ".";
    }
    cout << "*";
    for (k = loc; k < 78; k++) {
      cout << ".";
    }
    cout << endl;
  }
  return 0;
}                               // main 

$ ./a.out 
..........................................*.....................................
...........................................*....................................
..........................................*.....................................
...........................................*....................................
..........................................*.....................................
...........................................*....................................
..........................................*.....................................
...........................................*....................................
...........................................*....................................
..........................................*.....................................
...........................................*....................................
...........................................*....................................
..........................................*.....................................
.........................................*......................................
.........................................*......................................
........................................*.......................................
........................................*.......................................
.........................................*......................................
........................................*.......................................
.......................................*........................................
........................................*.......................................
.......................................*........................................
......................................*.........................................
......................................*.........................................
......................................*.........................................
......................................*.........................................
......................................*.........................................
.....................................*..........................................
....................................*...........................................
.....................................*..........................................
.....................................*..........................................
......................................*.........................................
......................................*.........................................
.......................................*........................................
........................................*.......................................
.........................................*......................................
.........................................*......................................
..........................................*.....................................
...........................................*....................................
............................................*...................................

But can be simplified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(nullptr)));

	for (int i = 0, loc = 40; i < 40; ++i) {
		loc += rand() % 3 - 1;
		cout << setw(loc) << setfill('.') << '.' << "*" << setw(79 - loc) << setfill('.') << "\n";
	}
}

Their are numbers that suppose to go on top of the output.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <sstream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
   stringstream L1, L2;
   for ( int i = 1; i <= 80; i++ )
   {
      L1 << i / 10;
      L2 << i % 10;
   }
   cout << L1.str() << '\n' << L2.str() << '\n';
   
   srand( time( 0 ) );
   for ( int i = 0, loc = 40; i < 40; i++ )
   {
      loc += rand() % 3 - 1;
      cout << string( loc - 1, '.' ) + "*" + string( 80 - loc, '.' ) << '\n';
   }
}
Last edited on
Topic archived. No new replies allowed.