Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Rectangle Program

1. ## Rectangle Program

Hi all, I am currently trying to create a code that will allow me to do the following:

Enter length vs. width of a rectangle

Receive area + perimeter values

I was given a solution template to get me started:

/* File rectangle.c
Given the dimensions of a rectangle in metres, compute its area and
perimeter length.

Programmer: Date:

*/

#include <stdio.h>

int main(void)
{
int x, y, area, perimeter;

printf("Dimensions of the rectangle (rounded to nearest metre): ");

/* you fill in the rest */

system("pause"); /* omit this line on Mac's */

return 0;
}

I cannot seem to figure this out with the given template, however I was able to create a code that allows me to get the area by entering l*w and all I would have to do from there was add another code which will allow the user to input 2 lengths and receive the perimeter. But I am worried my professor will expect me to use his template.

Here is MY version of the code which is half complete:

#include <iostream>

using namespace std;

int mult ( int l, int h );

int main()
{
int x, y, area, perimeter;

printf("Dimensions of the rectangle (rounded to the nearest metre) : ");

int l;
int h;

cout<<"Enter length, height: ";
cin>> l >> h;
cin.ignore();
cout<<"The area of your rectangle is "<< mult ( l, h ) <<"\n";
}

int mult ( int l, int h )
{
return l * h;
}

And here is the one I attempted off of his template:

/* File rectangle.c
Given the dimensions of a rectangle in metres, compute its area and
perimeter length.

Programmer: Date:

*/

#include <stdio.h>

int main(void)
{
int x, y, area, perimeter;

printf("Dimensions of the rectangle (rounded to nearest metre): ");

int mult ( int l, int h );

int main()
{
int x, y, area, perimeter;

printf("Dimensions of the rectangle (rounded to the nearest metre) : ");

int l;
int h;

cout<<"Enter length, height: ";
cin>> l >> h;
cin.ignore();
cout<<"The area of your rectangle is "<< mult ( l, h ) <<"\n";
}

int mult ( int l, int h )
{
return l * h;
}

I basically just migrated them. I have zero experience with C++, It's just a req in my first year of engineering. Any pointers will be greatly appreciated. Thanks!

system("pause"); /* omit this line on Mac's */

return 0;
}

• Originally Posted by tylergray
#include <iostream>

using namespace std;
...
cout<<"Enter length, height: ";
cin>> l >> h;
...
}
The comment in the template says the file is named with a .c extension, which makes it a C source file. But the code includes stuff that's only available in C++.

Is your assignment to write C or C++?

• Looks like C++ code to me, taken from the ideas of a source file from C. If this was C, you would be able to use tricks with printf() and memset:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int height = 15;
int width = 30;
char sym = '#';

char layer[width+1];
memset (layer, sym, width+1);
printf("%s\n", layer);
for (int i = 0; i < height; i++)
{
printf ("%c%*c \n", sym, width, sym);
}
printf("%s\n", layer);
return 0;
}```
With C++, I would use a string and make use of the constructor that allows you to allocate a char of a certain repetition to be set, and just some string manipulation and loops.

Code:
```std::string s(25, '*');
// Output 25 *'s: *************************
std::cout << s << std::endl;```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•