Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 921081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:49:04+00:00 2026-05-15T18:49:04+00:00

I built a little program that calculates the average of 15 numbers or less.

  • 0

I built a little program that calculates the average of 15 numbers or less. There are 15 text-boxes, each one’s default value is ‘0’. The program knows to get the sum of all typed numbers and divide it by numbers of text boxes that don’t return ‘0’. But if the user deletes in mistake one of the ‘0’os in one of the text boxs.. run-time error.

Originally I solved this problam by writing this “if statement” 15 times(one for each text-box):

 if (t1.Text == "") { tr1 = 0; }
 else
 {
     tr1 = Double.Parse(t1.Text);
 }

this code checks if there isn’t a thing in text box(for example, named t1), if true, the program is giving the double ‘tr1′(don’t confuse with ‘t1’), the value of ‘0’, if false, the code gives the double ‘tr1’ the text of ‘t1’.

i had to write this ‘if’ 15 times. i wanted to know if i can write the same code with arrays and a for loop, and how?

here is the whole code (sorry for var names are not similar to var’s use.):

 private void goyouidiot_Click(object sender, EventArgs e)
 {
     double tr1;
     double tr2;
     double tr3;
     double tr4;
     double tr5;
     double tr6;
     double tr7;
     double tr8;
     double tr9;
     double tr10;
     double tr11;
     double tr12;
     double tr13;
     double tr14;
     double tr15;
     if (t1.Text == "") { tr1 = 0; }
     else
     {
         tr1 = Double.Parse(t1.Text);
     }
     if (t2.Text == "") { tr2 = 0; }
     else
     {
         tr2 = Double.Parse(t2.Text);
     }

     if (t3.Text == "") { tr3 = 0; }
     else
     {
         tr3 = Double.Parse(t3.Text);
     }


     if (t4.Text == "") { tr4 = 0; }
     else
     {
         tr4 = Double.Parse(t4.Text);
     }


     if (t5.Text == "") { tr5 = 0; }
     else
     {
         tr5 = Double.Parse(t5.Text);
     }

     if (t6.Text == "") { tr6 = 0; }
     else
     {
         tr6 = Double.Parse(t6.Text);
     }


     if (t7.Text == "") { tr7 = 0; }
     else
     {
         tr7 = Double.Parse(t7.Text);
     }


     if (t8.Text == "") { tr8 = 0; }
     else
     {
         tr8 = Double.Parse(t8.Text);
     }

     if (t9.Text == "") { tr9 = 0; }
     else
     {
         tr9 = Double.Parse(t9.Text);
     }


     if (t10.Text == "") { tr10 = 0; }
     else
     {
         tr10 = Double.Parse(t10.Text);
     }


     if (t11.Text == "") { tr11 = 0; }
     else
     {
         tr11 = Double.Parse(t11.Text);
     }


     if (t12.Text == "") { tr12 = 0; }
     else
     {
         tr12 = Double.Parse(t12.Text);
     }

     if (t13.Text == "") { tr13 = 0; }
     else
     {
         tr13 = Double.Parse(t13.Text);
     }


     if (t14.Text == "") { tr14 = 0; }
     else
     {
         tr14 = Double.Parse(t14.Text);
     }


     if (t15.Text == "") { tr15 = 0; }
     else
     {
         tr15 = Double.Parse(t15.Text);
     }
     double[] sch = { tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9, tr10, tr11, tr12, tr13, tr14, tr15 };
     double total = 0;
     double sorf = 0;
     for (int i = 0; i != 14; i++)
     {

         sorf = sorf + sch[i];

         if (sch[i] > 0)
         { total++; }

     }

     double totalic = sorf / total;
     string glass = totalic.ToString();
     result.Text = ("your score: " + glass);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:49:04+00:00Added an answer on May 15, 2026 at 6:49 pm
    Double.TryParse(t1.Text.Trim(), out tr1);
    

    will set tr1 to the text box’s numeric value, or 0.0 if it failed to convert it for some reason. It’ll also return true if the conversion succeeded or false if it failed, but you don’t care about the return value if the default is 0.0.

    Added bonus: it won’t throw an exception if someone decides to put “This is not a number.” into a text box. It’ll just see the value as 0.

    To do this in an array…

    TextBox t[] = { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15 };
    double tr[] = new double[t.Length];
    
    for (int i = 0; i < t.Length; ++i)
    {
        Double.TryParse(t[i].Text.Trim(), out tr[i]);
    }
    

    UPDATE:

    Note, it’s perfectly reasonable to expect to be able to compute an average of numbers that includes 0. In order to do this:

    TextBox t[] = { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15 };
    double tr[] = new double[t.Length];
    int valid_count = 0;
    
    for (int i = 0; i < t.Length; ++i)
    {
        if (Double.TryParse(t[i].Text.Trim(), out tr[i])) ++valid_count;
    }
    

    Set your TextBoxes’ default values to blank (“”), and then you’ll know how many were legitimately 0’s entered by the user and how many were blank. Divide the sum by valid_count to get an accurate average. (But be sure valid_count > 0, or you’ll likely get a divide-by-zero exception.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I've written a neat little program that can analyse Japanese text and give
I built a little installer, which installs to program files. I run the installer
Ive built a little jquery sliding div on my website that when you hover
I've built a web site on ASP.NET MVC and one little section of it
I've got an little canvas tool I've built that cut up images into little
Is there a built in linux utility that I can use to test a
I have created a little program that reads in a Java file and feeds
I try to make a little program that sorts an array using threads but
I'm trying to build a little renaming program to help save me time in
I have built a little web application with several *.js files. I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.