473,626 Members | 3,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error of "use of unassigned local variable"

2 New Member
Hi, I am new in learning c#, and have recently encountered a problem:


Expand|Select|Wrap|Line Numbers
  1.         static float ComputeAvg(float[] a)
  2.         {
  3.             float sum;
  4.             int i;
  5.             for (i = 0; i <= a.Length; i++)
  6.             {
  7.                 sum = sum + a[i];
  8.             }
  9.  
  10.             float avg = sum / (i-1);
  11.  
  12.             return avg;
  13.         }
It comes with a exception says "sum" is "use of unassigned local variable". Since I have already declared "sum" outside the "for" loop, why it says "sum" is unassigned? Thanks!
Mar 24 '10 #1
2 6425
tlhintoq
3,525 Recognized Expert Specialist
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

Since I have already declared "sum" outside the "for" loop, why it says "sum" is unassigned?
First let me compliment you on your use of 'declared' versus 'assigned'. A lot of people get those mixed up. And your answer is in those two different terms.

You have 'declared' that the variable "sum" exists and that it is a float. But you have not 'assigned' it any value. It is always a good practice to assign a starting value when you create a variable.

Expand|Select|Wrap|Line Numbers
  1. float sum = 0.0f;
Mar 24 '10 #2
plasmay
2 New Member
Thank you very much. I have found the answer right after I posted my question... Since I know a little about VB, and it will assign an default value for var when I didnt do so, so I was supposed...you know... Anyway, thanks for your reply. have a good night.
Mar 24 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3258
by: Ian U | last post by:
Hello, I'm looking for a "Set Up Your Permissions 101" in response to the above error, which I get on a local IIS5.1 installation, when I'm trying to view Live Data / preview an ASP file from within Dreamweaver MX. I get the above error, which is 'dealt with' in http://support.microsoft.com/default.aspx?scid=kb;en-us;174943 , but I'm not wise enough to be sure about the correct process for ensuring that the IUSR_<machinename> account...
24
2853
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have a class called polynomial. Its a nothing little class right now, with just int variables, a basic container class. Im using it as I go through some tutorials, but in this particular tutorial its telling me to do polynomial *first = new...
3
1705
by: Zeljko | last post by:
Hi, Is there a way to define a local variable that retains it's value between the function calls ? Zeljko
14
10775
by: Richard Thornley | last post by:
Hello, I realize that this is a very basic question but is there an advantage to using Me.ControlName.Text over just using ControlName.Text? I have been leaving the Me. off but would include it if there are benefits. Thanks, Richard
4
11344
by: octavio | last post by:
Hello members of the comp.lang.c newsgroup. Please I need you help on the following one. Compiling the simple code I'm getting this error message. Why ? Please what's the correct type of the fb function ? Thank You. Octavio and.c:15: attention : type mismatch with previous implicit declaration and.c:11: attention : previous implicit declaration of `fb'
60
5017
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
8
11141
by: Dom | last post by:
This is a little tricky, but I think the error I'm getting isn't valid. The compiler just doesn't know my logic. MyObject o; switch (IntVariable) { case 1: o = new MyObject() break;
3
2840
by: Adrian Smith | last post by:
While waiting for my paid-for web-hosting company to get back to me about my difficulties running python scripts on their servers... http://groups.google.com/group/comp.lang.python/browse_frm/thread/39b52bcf0dffec4c/4ff805bf283acc15?lnk=gst&q=adrian+smith&rnum=1&hl=en#4ff805bf283acc15 ....I went and found a free one as a stopgap. And it worked! I was as happy as a clam. But then it stopped working. Ah. I stripped the script down to the same...
3
6008
by: Larry Leonard | last post by:
Running MSDE 2000 SP2 on Windows XP SP3. I have a T-SQL script that is relatively simple (adding constraints, inserting rows, etc.) and short (maybe 300 lines, heavily commented). It's an upgrade script I wrote weeks ago, and has worked fine everywhere. After the patch from last Wednesday. "Security Update for SQL Server 2000 Service Pack 4 (KB948110)", I have gotten the following error on two different machines, using a copy of the...
0
8272
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.