New to c#

New to c#

Post by SaneSmile on Wed Sep 08, 2010 12:19 pm
([msg=45283]see New to c#[/msg])

Hi im new to c# as ive recently started learning it at a-level and my friends have told me about this site,
and i was wondering if any one could recomend what i should read to help me learn?
also im having a bit of trouble, we have only had one lesson so far but the teacher had to rush off on a personl emergency
leaving a sub to try to explain the basics to us, we were set a peice of homework to write a program that calulats the area of a rooms four walls and its ceiling, and i was wondering if anyone could tell me what ive done wrong?


using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace paint_calulator
{
class program
{
Static void main(string[] args)
{

double a;
double b;
double c;
double d;
double e;

a = 100;
b = 34;
c = 60;

height = b;
width = c;
length = a;
wallarea = e;
ceilingarea = d;

wallarea = (height * width) * 2 + (height * length) * 2;
ceilingarea = length * width;
totalarea = wallarea + ceilingarea;

console.write (totalarea);
console.read();
Last edited by SaneSmile on Wed Sep 08, 2010 12:40 pm, edited 1 time in total.
SaneSmile
New User
New User
 
Posts: 8
Joined: Tue Sep 07, 2010 1:20 pm
Blog: View Blog (0)


Re: New to c#

Post by mojo1948 on Wed Sep 08, 2010 12:33 pm
([msg=45284]see Re: New to c#[/msg])

At first glance, I'd say that your height variable is spelled wrong.
Code: Select all
wallarea = (heigh * width) * 2 + (heigh * length) * 2;


I don't know c# ,but if this is the right way to assign variables in c# I won't like it.
Code: Select all
double a;
double b;
double c;
double d;
double e;

a = 100;
b = 34;
c = 60;

heigth = b;
width = c;
length = a;
wallarea = e;
ceilingarea = d;
Never stop learning.
User avatar
mojo1948
Experienced User
Experienced User
 
Posts: 60
Joined: Sun Jul 18, 2010 5:45 am
Blog: View Blog (0)


Re: New to c#

Post by cilpolir on Wed Sep 08, 2010 12:47 pm
([msg=45285]see Re: New to c#[/msg])

mojo1948 wrote:At first glance your height variable is spelled wrong.<br>
Code: Select all
wallarea = (heigh * width) * 2 + (heigh * length) * 2;

you also have to declare the variables: height,width, lenght, wallarea, ceilingarea and totalarea, and because of that you can delete the lines with double a-e, just do it like:
Code: Select all
double height;
double lenght;
double etc;

height = 123;
lenght=456;


I would declare the same kind of variables in the same line
like:
Code: Select all
int a,b,c,d,e,height,width,lenght,etc;

Also I would use int, or float instead of dubble.
Just me though.
Image
User avatar
cilpolir
Poster
Poster
 
Posts: 214
Joined: Sat Sep 12, 2009 10:46 am
Blog: View Blog (0)


Re: New to c#

Post by SaneSmile on Wed Sep 08, 2010 2:16 pm
([msg=45291]see Re: New to c#[/msg])

thank you, i didnt learn about int in class and the spelling mistake was really something i should have seen ... 8-)
SaneSmile
New User
New User
 
Posts: 8
Joined: Tue Sep 07, 2010 1:20 pm
Blog: View Blog (0)


Re: New to c#

Post by mRmasteRful on Wed Sep 08, 2010 2:21 pm
([msg=45292]see Re: New to c#[/msg])

I dont know C# i only know C. ANyway should an if statement be placed to account if the room is a square
Code: Select all
if (length==width)


Or maybe you didnt learn this yet?
mRmasteRful
Poster
Poster
 
Posts: 274
Joined: Fri Apr 02, 2010 11:53 pm
Blog: View Blog (0)


Re: New to c#

Post by Goatboy on Wed Sep 08, 2010 2:27 pm
([msg=45293]see Re: New to c#[/msg])

mRmasteRful, that would technically work, but what would be the point?
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2806
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: New to c#

Post by mRmasteRful on Wed Sep 08, 2010 2:35 pm
([msg=45294]see Re: New to c#[/msg])

I was looking at my room and it looks almost square, almost. There is no reason but I like to cover all the angles when Im solving problems in C. My teacher got me in that habit.
mRmasteRful
Poster
Poster
 
Posts: 274
Joined: Fri Apr 02, 2010 11:53 pm
Blog: View Blog (0)


Re: New to c#

Post by SaneSmile on Wed Sep 08, 2010 2:48 pm
([msg=45296]see Re: New to c#[/msg])

the problem was just to write a program that could calulate the area of a room given the width, length and height
which now works :D
Last edited by SaneSmile on Wed Sep 08, 2010 2:50 pm, edited 1 time in total.
SaneSmile
New User
New User
 
Posts: 8
Joined: Tue Sep 07, 2010 1:20 pm
Blog: View Blog (0)


Re: New to c#

Post by Sector on Wed Sep 08, 2010 2:50 pm
([msg=45297]see Re: New to c#[/msg])

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace area_calc
{
    class Program
    {
        static void Main(string[] args)
        {
            #region :: Variables ::

            // wall variables
            double wallHeight, shortWallWidth, longWallWidth;
            // ceiling variables   
            double ceilLength, ceilWidth;
            // area variables
            double totalArea, longWallArea, shortWallArea, ceilArea;
            // volumne variables
            double roomVolumne;

            #endregion

            Console.WriteLine("Area calculator!\r\n================\n\n");
            Console.Write("Please enter the height of the walls: ");
            // user to input value of wallHeight...
            wallHeight = Convert.ToDouble(Console.ReadLine());

            Console.Write("Please enter the width of the longest walls: ");
            // ... likewise for the width of the longest wall...
            longWallWidth = Convert.ToDouble(Console.ReadLine());

            Console.Write("Please enter the width of the shortest walls: ");
            // ... and the shortest...
            shortWallWidth = Convert.ToDouble(Console.ReadLine());

            /*
             * Assign the ceiling variables based on the values obtained for the walls
             * This (foolishly) assumes that the ceiling wants to fit on the room...
             */

            ceilLength = longWallWidth;
            ceilWidth = shortWallWidth;

            // set area variables
            // 2 wall heights by 2 wall widths...
            longWallArea = ((longWallWidth  * wallHeight ) * 2);
            // ... same for the short walls
            shortWallArea = ((shortWallWidth * wallHeight ) * 2);
            // area of the ceiling
            ceilArea = (ceilLength * ceilWidth);
            // total area of all four walls, plus the ceiling AND the floor (hence ceilArea * 2)
            totalArea = longWallArea + shortWallArea + ( ceilArea * 2 );
            // the room volumne, for the hell of it
            roomVolumne = longWallWidth * shortWallWidth * wallHeight;

            // check if the width of both walls and the ceiling match...
            // ... and then print out the rest of the data as needed
            if ((longWallWidth == shortWallWidth) && (longWallWidth == wallHeight))
            {
                Console.WriteLine("You appear to be designing a cube!");
                Console.WriteLine("The area of one of your walls is: {0}", longWallArea / 2);
                Console.WriteLine("The area of all your walls is: {0}", shortWallArea + longWallArea);
            }
            else
            {
                Console.WriteLine("The area of both of the longest walls is: {0}", longWallArea);
                Console.WriteLine("The area of both of the shortest walls is: {0}", shortWallArea);
            }

            Console.WriteLine("The area of the ceiling is: {0}", ceilArea);
            Console.WriteLine("Total area: {0}", totalArea);
            Console.WriteLine("The volumne of the room is: {0}", roomVolumne);

            Console.ReadLine();
        }
    }
}


I put this together for you, and have kept it as simple as I could considering this was your first lesson on C#.
If you don't understand what something does, I hope you google it and develop your understanding a little further rather than just thinking 'ah, awesome... someone has done it for me'.

There is quite a lot there to be reading up on for a first lesson on C# anyway.

Sorry if I have broken any forum rules by giving the OP an answer, I have tried to make it as tutorial-esque as possible.

-- Wed Sep 08, 2010 2:55 pm --

@the OP: I left in a way for you to easily break this program too, find it :)
Last edited by Sector on Sat Sep 18, 2010 12:11 am, edited 1 time in total.
Sector
New User
New User
 
Posts: 30
Joined: Mon Jul 19, 2010 5:58 am
Blog: View Blog (0)


Re: New to c#

Post by SaneSmile on Wed Sep 08, 2010 2:57 pm
([msg=45299]see Re: New to c#[/msg])

@the OP: I left in a way for you to easily break this program too, find it :)[/quote]

I think ive found 'The break' when you input the data the variables effect the output so i used 12, 4 and -7000
which gave out negative values for the ceiling the shortest wall, the total area and the volume

i think that you could have used an if statment after the data is entered to make sure that theirs no invalid data?
what you think?
SaneSmile
New User
New User
 
Posts: 8
Joined: Tue Sep 07, 2010 1:20 pm
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests