I tried this problem but i keep getting errors, how do i fix them

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 6784 Views - Last Post: 06 January 2012 - 09:44 PM Rate Topic: -----

#1 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

I tried this problem but i keep getting errors, how do i fix them

Posted 01 January 2012 - 12:47 PM

This is my first question that i've posted so i don't know if im doing it right, sorry if not. And i don't know how to put it in the format that you guys want. Sorry.

Homework instructions said:

Write A Rainfall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following:
the total rainfall for the year
the average monthly rainfall
the month with most rain
the month with least rain
Demonstrate the class in a complete program. (Do not accept negative numbers for monthly rainfall figures)

Read more: Write A Rainfall class that stores the total rainfall for each - JustAnswer http://www.justanswe...l#ixzz1iEmCnbcu


using System;

class RainFall
{
	public static void Main()
	{
		double totalRainFall;
		double averageRainFall;
		double mostRain;
		double leastRain;
		
		totalRainFall();
		averageRainFall();
		mostRain();
		leastRain();
	}
	
	public static double TotalRainFall()
	{
		double[] rainfall = new double [12];


I am stuck from here, i don't know like how am i going to put an array with this, i am so confused on how to approach this. Thank you for any one that helps.

its in c# please, i tried finding it online but its in java and im even more confused

This post has been edited by tlhIn`toq: 01 January 2012 - 12:50 PM
Reason for edit:: code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I tried this problem but i keep getting errors, how do i fix them

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 01 January 2012 - 12:53 PM

Other than "I'm confused"... What exactly is your question?

Since the instructions read like homework we can't give you code, but can guide you to some better understanding. But with the post being so vague I'm not sure where to start.

You have already created an array, so that doesn't seem to be a problem. From here you just need to keep coding. Make a method for each of the assignment requirements.


Reminder to new folks: This is homework and we don't provide completed code for homework. Rather we try to help the student comprehend the concept they are struggling with so they can write their own code. They are being graded on their skill not ours.

Stages of asking for homework help on a forum.
I don't want you to write my code, just give me ideas on how to solve my problem.

What have to you tried to code so far?
What part of what you have tried isn't working?
Are you getting error messages, or is it just not giving you the results you wanted?

(In other words, you at least have to make and show us your good faith effort to do your own coding. We are here to help you, not do it for you.)
What does this error mean?

Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.
Was This Post Helpful? 0
  • +
  • -

#3 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 01 January 2012 - 12:54 PM

i don't get where to start like, where do i put in the arrays do i put it in the the methods or how would i format the arrays, this is not homework, i failed programming this year in university and i am trying to learn it on my own now through the textbook questions. Okay how would i start like can someone just tell me what to write in the methods or something? or what do i put in the private sections?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 01 January 2012 - 01:32 PM

"the private sections"... Uh... Those are the methods (also known as functions).

No offense, but I don't think anyone here is going to be your free private tutor to learn C# from the ground up. I certainly have no desire to sit here and write all of your methods for you. That is what the assignment wants you to do. If you are not ready for that assignment then back up one or two or five chapters and re-read and re-work the lessons. There is no point you being given the code for this if you don't understand what you're doing; that won't actually help you in the long term.

We have several tutorials in the C# section. And there are MANY good self-teaching books on the subject.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Dec 2011
Spoiler


==========================

Quote

i am trying to learn it on my own now through the textbook questions.


So you are re-using the same textbook from class? Don't do that. Go pick up "Head First C#" or "Learn C# in 24 hours" and work those books from cover to cover.

Those books are designed for the self-teaching student. Where you class textbook is just one part of the curriculum and is expected to be augmenting the live lecture from the teacher.
Was This Post Helpful? 0
  • +
  • -

#5 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 01 January 2012 - 01:39 PM

thank you for your help
Was This Post Helpful? 0
  • +
  • -

#6 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 01 January 2012 - 05:52 PM

Write A Rainfall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following:
the total rainfall for the year
the average monthly rainfall
the month with most rain
the month with least rain
Demonstrate the class in a complete program. (Do not accept negative numbers for monthly rainfall figures)

using System;

class RainFall
{
	private double total = 0;
	private double average;
	private double most;
	private double least;
	private double[] month = new double[11]; 
	private double amount;

	public RainFall(double t, double a, double m, double l, double w)
	{
		total = t;
		average = a;
		most = m;
		least = l;
		amount = w;
	}
	
	public double Amount()
	{		
		for (int i = 0; i < month.Length; i++)
		{
			Console.WriteLine("Enter the rainfall (in inches) for month {0}", (i+1));
			month[i] = Convert.ToDouble(Console.ReadLine());
			while (month[i] <= 0)
			{
				Console.WriteLine("Enter the rainfall (in inches) for month {0}", (i+1));
				month[i] = Convert.ToDouble(Console.ReadLine());
			}
			
			return month;
		}
	}
	
	public double Total()
	{
		for (int i = 0; i < month.Length; i++)
		{
			total += month[i];
			return total;
		}
	}
	
	public double Average()
	{
		for (int i = 0; i < month.Length; i++)
		{
			average = total / 12;
		}
	}
	
	public double Most()
  	{
        double most = 0;
		
        for ( int i = 0; i < month.Length; i++)
        {
        	if (month[i] > most)
        	{
        		most = month[i] ;
        	}
        }
        return most;
  	}

	public double Least()
	{
		double least = 0;
		
		for ( int i = 0; i < month.Length; i++)
		{
			if (month[i] < least)
			{
				least = month[i];
			}
		}
		return least;
	}
	
	public static void Main()
	{
		Console.WriteLine("The total rainfall was {0}.", total);
		Console.WriteLine("The average rainfall was {0}.", average);
		Console.WriteLine("The most rainfall was {0}.", most);
		Console.WriteLine("The least rainfall was {0}.", least);
	}	
}		



My errors are
<33,16> cannot implicitly convert type 'double[]" to 'double'
<39, 37> warning CS0162: unreachable code detected
<37, 16> error CS0161: 'RainFall.Average<>': not all code paths return a value
<46,16> error CD0161: "RainFall.Average<>": not all code paths return a value.
<84, 52> error Cd0120: an object reference is required for the non-static field method, or property 'RainFall.total'
<5, 17> <Location of symbol related to previous error>
<85, 54> error CD0120: An object reference is required for the non - static field, method, or property 'RainFall.average'
,6, 17.: <Location of symbol related to previous error>
<86, 51> error CD0120: An object reference is required for the non- static field, method, or property 'RainFall.most'
<7, 17> <Location of symbol related to previous error>
<87, 52>: error CD0120: An object reference is required for the non- static field, method, or property 'RainFall.least'
<8, 17> <Location of symbol related to previous error>

I have no clue how to fix these it took me around 6 hours to write this code and I tried reading in the textbook and I am now stuck here. Please help me. Thank You.
Was This Post Helpful? 0
  • +
  • -

#7 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 01 January 2012 - 06:19 PM

You really need to try harder for yourself, since the errors here are obvious... :P

Here are the solutions to your mentioned errors (yes there are more, as you will see for yourself later... :D)
Let's go in order:

1.

Quote

cannot implicitly convert type 'double[]" to 'double'


You need to specify that you will return a double array, buy you're just saying you will return a double:
So change this:
public double Amount(){/*...*/
Into this:
public double[] Amount(){/*...*/}

Why is this happening? Well when you return a array of doubles (hint: return month (and 'month' is an array of doubles), you need to specify that you will be returning it in the method signature.

2.

Quote

<37, 16> error CS0161: 'RainFall.Average<>': not all code paths return a value
<46,16> error CD0161: "RainFall.Average<>": not all code paths return a value.


You need to return your value after the for loop:

So change this:

public double Average()
	    {
	        for (int i = 0; i < month.Length; i++)
	        {
	            average = total / 12;
	        }
	    }



Into this:
public double Average()
            {
                for (int i = 0; i < month.Length; i++)
                {
                    average = total / 12;
                }
                return average;
            }



Why is this happening? Well if you say you will return a double, then you have to return a double. In your code you didn't return anything.

NOTE: You have more errors, as you will probably discover for yourself... :D
NOTE2: Some of your logic isn't good, I won't tell you what part, you will figure that part out for yourself ;)
NOTE3: Maybe look toward improving your code style... :D
Question: What IDE are you using? You're not using any version of VS from what I see. A GREAT and FREE IDE is the
Visual C# 2010 Express from Microsoft. So yeah, this is a suggestion to change your IDE. :D

FINAL AND MOST IMPORTANT NOTE: Since you're pretty new to all this programming 'stuff' (at least from what I see), take a look at these two GREAT tutorials to get you started:
Building an application - POS/Cash Register - Part one Custom keyboard and inheritence
Building An Application - POS/Cash Register - Part Two The Display :D

OR Grab a book and read it from COVER TO COVER. My suggestion and personal favorite is: Microsoft Visual C# 2010 Step by Step, or if you don't like that one chose one from out: Recommended C# Books

DON'T POST DUPLICATE QUESTIONS! :D
I have no clue how to start this code can any one point out what to do
You should've just continued to post your questions in that thread. Since it is the same project AND probably the same problem.

Resolved with the merge of the thread. :D

This post has been edited by RexGrammer: 01 January 2012 - 06:43 PM

Was This Post Helpful? 2
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 01 January 2012 - 06:44 PM

In the future please don't start multiple threads for the same topic.
I've merged the two threads for this one issue.

I'm going to repeat my earlier suggestion:
Work on tutorials where they walk you through exercises to help you learn. Stop trying to create something from scratch - You're not ready to do that.
Was This Post Helpful? 0
  • +
  • -

#9 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 02 January 2012 - 12:16 PM

I've tried going through those, the only way im learning is through struggling to write code from scratch, the thought process i guess is helping me, i've tried reading sooo much and watching videos but honestly im not learning until i actaully type it.

how come in this code, i did not put least and greatest in my private fields?

using System;

public class RainfallArray
{
	private int month = 12;
	private double total = 0;
	private double average;
	private double[] months = new double[12];
	
	public void setMonths()
	{
         for(int i = 0; i  < month; i++ )
         {
			 Console.WriteLine("Enter the rainfall (in inches) for month #{0}: ", (i+1));
			 months[i] = Convert.ToDouble(Console.ReadLine());
			 
			//Input Validation - Cannot accept a negative number
			while (months[i] < 0)
			{
				Console.WriteLine("Oops! Rainfall must be at least 0. Please enter a new value.");
				Console.WriteLine("Enter the rainfall (in inches) for month #{0}: ", (i+1));
				months[i] = Convert.ToInt32(Console.ReadLine());
			}
         }
	}

	public double Total()
	{
		total = 0;
		
		for(int i = 0; i < 12; i++)
		{
			total = total + months[i];
		}
		return total;
  	}
  
	public double Average()
	{
		average = total / 12;
		return average;
	}

	public double Largest()
	{
		double largest = months[0];//first element of the array
		 
		for (int i = 0; i < months.Length; i++)
		{
			if (months[i] > largest)//checks if next number is greater the preceding                     
								 //one
			{
				largest = months[i];
			}
		}
		return largest;
	}

	public double Least()
	{
    double least = months[0];//first element of the array
	
    for (int i = 0; i < months.Length; i++)
    {
        if (months[i] < least)//checks if next number is lesser the preceding                     
                             //one
        {
            least = months[i];
        }
    }
    return least;
	}

	public static void Main()
	{
		RainfallArray r =new RainfallArray();
		r.setMonths();
		Console.WriteLine("The total amount of rainfall was: {0}. ", r.Total());
		Console.WriteLine("The smallest amount of rainfall was: {0}. ", r.Least());
		Console.WriteLine("The largest amount of rainfall was: {0}. ", r.Largest());
		Console.WriteLine("The average amount of rainfall was: {0}. ", r.Average());
	}

}



Was This Post Helpful? 0
  • +
  • -

#10 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 02 January 2012 - 12:27 PM

You have declared local variables in your methods. You're using them instead of global variables (the ones you say are 'missing' :D) ;)

Example:

public double Largest()
{
     double largest = months[0];//Here is the variable you're using for the comparison
     //...
}


This post has been edited by RexGrammer: 02 January 2012 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#11 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 02 January 2012 - 12:32 PM

does that mean i can take out the private average and private total?
Was This Post Helpful? 0
  • +
  • -

#12 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 02 January 2012 - 12:43 PM

Of course, it would be good idea to do that...

But you have to move their declaration to the method what isn't hard:

public double Total()
	    {
	        double total = 0;
	         
	        for(int i = 0; i < 12; i++)
	        {
	            total = total + months[i];
	        }
	        return total;
	    }



public double Average()
	    {
	        double average = total / 12;
	        return average;
	    }



That way you can delete the declarations of the global variables called total and average.

This post has been edited by RexGrammer: 02 January 2012 - 12:44 PM

Was This Post Helpful? 0
  • +
  • -

#13 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 02 January 2012 - 12:51 PM

but if i do that then i get an error saying that total doesn't exist in current context.

using System;

public class RainfallArray
{
	private int month = 12;
	private double[] months = new double[12];
	
	public void setMonths()
	{
         for(int i = 0; i  < month; i++ )
         {
			 Console.WriteLine("Enter the rainfall (in inches) for month #{0}: ", (i+1));
			 months[i] = Convert.ToDouble(Console.ReadLine());
			 
			//Input Validation - Cannot accept a negative number
			while (months[i] < 0)
			{
				Console.WriteLine("Oops! Rainfall must be at least 0. Please enter a new value.");
				Console.WriteLine("Enter the rainfall (in inches) for month #{0}: ", (i+1));
				months[i] = Convert.ToInt32(Console.ReadLine());
			}
         }
	}

	public double Total()
	{
		double total = 0;
		
		for(int i = 0; i < 12; i++)
		{
			total = total + months[i];
		}
		return total;
  	}
  
	public double Average()
	{
		double average = total / 12;
		return average;
	}

	public double Largest()
	{
		double largest = months[0];//first element of the array
		 
		for (int i = 0; i < months.Length; i++)
		{
			if (months[i] > largest)//checks if next number is greater the preceding                     
								 //one
			{
				largest = months[i];
			}
		}
		return largest;
	}

	public double Least()
	{
    double least = months[0];//first element of the array
	
    for (int i = 0; i < months.Length; i++)
    {
        if (months[i] < least)//checks if next number is lesser the preceding                     
                             //one
        {
            least = months[i];
        }
    }
    return least;
	}

	public static void Main()
	{
		RainfallArray r =new RainfallArray();
		r.setMonths();
		Console.WriteLine("The total amount of rainfall was: {0}. ", r.Total());
		Console.WriteLine("The smallest amount of rainfall was: {0}. ", r.Least());
		Console.WriteLine("The largest amount of rainfall was: {0}. ", r.Largest());
		Console.WriteLine("The average amount of rainfall was: {0}. ", r.Average());
	}

}



Was This Post Helpful? 0
  • +
  • -

#14 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 02 January 2012 - 12:54 PM

Two things to fix/modify:

1. You should call the method total when you're determining the average.
2. Also you can use the additive assignment operator (+=) when counting the total.

NOTE:
This is:
total = total + months[i];

Is equal to:
total += months[i];

CODE:
Here's the modified code:
            public double Total()
	    {
	        double total = 0;
	         
	        for(int i = 0; i < 12; i++)
	        {
	            total += months[i];
	        }
	        return total;
	    }
	   
	    public double Average()
	    {
	        double average = this.Total() / 12;
	        return average;
	    }


This post has been edited by RexGrammer: 02 January 2012 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#15 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: I tried this problem but i keep getting errors, how do i fix them

Posted 02 January 2012 - 12:56 PM

public double Average()
37
    {
38
        double average = total / 12;
39
        return average;
40
    }

that total


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2