Convert into c++
Nov 5, 2020 at 4:21pm UTC
Hello can someone convert this code into c++ code
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include<stdio.h>
#include<malloc.h>
struct Room {
float length;
float width;
float squareFootage;
};
void show(struct Room room) {
printf("\nsquareFootage : %f" , room.squareFootage);
printf("\nLength : %f" , room.length);
printf("\nWidth : %f" , room.width);
}
float area(float length, float width) {
return length * width;
}
int main() {
int SIZE=0;
static struct Room* rooms;
printf("\nEnter the number of rooms in the apartment :" );
scanf("%d" , &SIZE);
rooms = (struct Room*) malloc(SIZE * sizeof (struct Room));
printf("\nEnter the dimensions for each room :\n" );
for (int i = 0; i < SIZE; i++) {
printf("\n\nRoom %d dimensions" , i + 1);
printf("\nLength :" );
scanf("%f" , &rooms[i].length);
printf("\nWidth :" );
scanf("%f" , &rooms[i].width);
rooms[i].squareFootage = area(rooms[i].length, rooms[i].width);
}
float apartmentSquareFootage = 0.0;
printf("\nDisplaying apartment details :\n" );
for (int i = 0; i < SIZE; i++) {
printf("\n\nRoom %d dimensions" , i + 1);
show(rooms[i]);
apartmentSquareFootage += rooms[i].squareFootage;
}
printf("\nApartment square footage :%f" , apartmentSquareFootage);
return 0;
}
Nov 5, 2020 at 4:29pm UTC
Fuck you, Chad. Do it yourself for once.
Nov 5, 2020 at 4:53pm UTC
I do what I can by my own.
I don't think you have to be rude, you weren't born a scientist
Nov 5, 2020 at 5:09pm UTC
Maybe you have never programmed with c++ facilities. Here your example coded with some basic c++ techniques. I hope, I made this not for letting you cheating or somewhat manner, but rather giving you a general impression how it could look like. I hope that you are willing and doing some own research if some functions or container classes are unclear to you how to use.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <vector>
#include <iostream>
struct Room {
float length;
float width;
float squareFootage() const { return length * width; }
};
// Overloads ostream operator<< for struct Room.
std::ostream & operator <<( std::ostream & os, const Room & room)
{
os << "\nsquareFootage : " << room.squareFootage()
<< "\nLength : " << room.length
<< "\nWidth : " << room.width;
return os;
};
int main() {
int SIZE=0;
std::cout << "\nEnter the number of rooms in the apartment :" ;
std::cin >> SIZE;
std::vector<Room> rooms( SIZE );
std::cout << "\nEnter the dimensions for each room: \n" ;
for ( size_t i = 0; i < rooms.size(); ++i) {
std::cout << "\n\nRoom " << i + 1 << " dimensions"
<< "\n\nLength :" ;
std::cin >> rooms[i].length;
std::cout << "\nWidth :" ;
std::cin >> rooms[i].width;
}
float apartmentSquareFootage = 0.0;
std::cout << "\nDisplaying apartment details :\n" ;
for (size_t i = 0; i < rooms.size(); i++) {
std::cout << "\n\nRoom " << i + 1 << " dimensions" ;
std::cout << rooms[i];
apartmentSquareFootage += rooms[i].squareFootage();
}
std::cout << "\nApartment square footage :" << apartmentSquareFootage;
return 0;
}
Nov 5, 2020 at 5:17pm 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <iostream>
#include <memory>
struct Room {
float length {};
float width {};
float squareFootage {};
};
std::ostream& operator <<(std::ostream& os, const Room& room) {
os << "\nsquareFootage : " << room.squareFootage;
os << "\nLength : " << room.length;
os << "\nWidth : " << room.width;
return os;
}
float area(float length, float width) {
return length * width;
}
int main()
{
int SIZE {};
std::cout << "\nEnter the number of rooms in the apartment :" ;
std::cin >> SIZE;
auto rooms {std::make_unique<Room[]>(SIZE)};
std::cout << "\nEnter the dimensions for each room :\n" ;
for (int i = 0; i < SIZE; ++i) {
std::cout << "\n\nRoom " << i + 1 << " dimensions" ;
std::cout << "\nLength :" ;
std::cin >> rooms[i].length;
std::cout << "\nWidth :" ;
std::cin >> rooms[i].width;
rooms[i].squareFootage = area(rooms[i].length, rooms[i].width);
}
float apartmentSquareFootage {};
std::cout << "\nDisplaying apartment details :\n" ;
for (int i = 0; i < SIZE; ++i) {
std::cout << "\n\nRoom " << i + 1 << " dimensions" ;
std::cout << rooms[i];
apartmentSquareFootage += rooms[i].squareFootage;
}
std::cout << "\nApartment square footage " << apartmentSquareFootage << '\n' ;
}
You want to change the output display.
Nov 5, 2020 at 5:43pm UTC
Thank you guys
Topic archived. No new replies allowed.