- 2

How to write a code for this convertion in C++?

The entered stars should be converted to poits. For example: Enter dimensions: 5 7 ******* ******* ******* ******* ******* Enter number of stars to convert: 21 ....... ....*** ******* ****... ....... End of program.

3rd Mar 2022, 9:15 PM
Nickan
11 Antworten
+ 1
Here is my code that I am trying to solve it. #include<iostream> #include<string> #include<iomanip> #include<algorithm> #include<iterator> #include<bits/stdc++.h> #include <cmath> using namespace std; void convert_point(int ant) { for(int i=0;i<ant;i++) cout<<"."; } void convert_star(int str) { for(int i=0;i<str;i++) cout<<"*"; } void print_figure(int r, int c, int stc) { for(int j=0; j<r;j++) { if(stc<=0) { for(int i=0;i<c;i++) { cout<<"*"; } } else if(stc>0) { double p=(stc/2.0); double pn{ceil(p)}; convert_point(pn); } cout<<endl; } } int main() { int rows, columns; int stars_to_convert; cout << "Enter dimensions: "; cin >> rows >> columns; print_figure(rows, columns, 0); cout << "Enter number of stars to convert: "; cin >> stars_to_convert; print_figure(rows, columns, stars_to_convert); cout
3rd Mar 2022, 9:46 PM
Nickan
+ 1
As you can see the problem is that i am trying to convert some of the stars not all of them. Then print half of them over and the remains under them.
4th Mar 2022, 8:20 AM
Nickan
0
G'day Nickan what have you already tried? Link your code bit? Or else can you come up with some psuedocode for the program?
3rd Mar 2022, 9:26 PM
HungryTradie
HungryTradie - avatar
0
https://code.sololearn.com/co4J7Ctl4zbk/?ref=app G'day mate, I adjusted the logic a bit, moved the check for stars before the array print, changed the function calls to print just one digit as per the array print, modified your way of halving the array and stars, etc etc. you may want to debug it much more thoroughly!
4th Mar 2022, 12:08 AM
HungryTradie
HungryTradie - avatar
0
G'day Manav Roy I think my version of Nickans code works the way it is supposed to. Would you have a look? Maybe do some more test cases for me?
4th Mar 2022, 9:01 AM
HungryTradie
HungryTradie - avatar
0
Yes but as you can see a simple input and output should be as this. As input you get a row and column number and then you get number that you want to convert them. For example The entered stars should be converted to poits. For example: Enter dimensions: 5 7 ******* ******* ******* ******* ******* Enter number of stars to convert: 21 ....... ....*** ******* ****... ....... End of program. There are 21 points but half of point should be printed over and rest of them under.
4th Mar 2022, 9:41 AM
Nickan
0
G'day Manav, it is Nickans code, I just moved some parts around. Yes, the print (or cout in C++) could be done with an expression rather than using a function, but that was Nickans code so I left it there. I had a lot of trouble with the initial print wanting all asterisks yet stc was =0. I should have just sent (row*column) to the function in the stc place.
4th Mar 2022, 9:42 AM
HungryTradie
HungryTradie - avatar