Programming Assisgnment Help
Mar 16, 2021 at 4:27pm UTC
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
Mar 16, 2021 at 5:19pm UTC
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
..........................................*.....................................
...........................................*....................................
..........................................*.....................................
...........................................*....................................
..........................................*.....................................
...........................................*....................................
..........................................*.....................................
...........................................*....................................
...........................................*....................................
..........................................*.....................................
...........................................*....................................
...........................................*....................................
..........................................*.....................................
.........................................*......................................
.........................................*......................................
........................................*.......................................
........................................*.......................................
.........................................*......................................
........................................*.......................................
.......................................*........................................
........................................*.......................................
.......................................*........................................
......................................*.........................................
......................................*.........................................
......................................*.........................................
......................................*.........................................
......................................*.........................................
.....................................*..........................................
....................................*...........................................
.....................................*..........................................
.....................................*..........................................
......................................*.........................................
......................................*.........................................
.......................................*........................................
........................................*.......................................
.........................................*......................................
.........................................*......................................
..........................................*.....................................
...........................................*....................................
............................................*...................................
Mar 16, 2021 at 6:00pm UTC
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" ;
}
}
Mar 16, 2021 at 6:28pm UTC
Their are numbers that suppose to go on top of the output.
Last edited on Mar 16, 2021 at 6:29pm UTC
Mar 16, 2021 at 7:09pm UTC
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 Mar 16, 2021 at 7:43pm UTC
Topic archived. No new replies allowed.