Click here to Skip to main content
14,669,422 members
Rate this:
Please Sign up or sign in to vote.
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_Week6
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[100];
            int i, n, sum = 0;
            int average = 0;
           
            int yes = 0;
          
           

            Console.Write("\n\nRead and Print elements of an array:\n");
            Console.Write("-----------------------------------------\n");

            Console.Write("Input the number of elements to be stored in the array :");
            n = Convert.ToInt32(Console.ReadLine());

            Console.Write("Input {0} elements in the array :\n", n);
            for (i = 0; i < n; i++)
            {
                Console.Write("element - {0} : ", i);
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (i = 0; i < n; i++)
            {
                sum += arr[i];
            }

            for (i = 0; i < 2; i++)
            {
                yes = i;
            }
            average = sum / n;
            Console.WriteLine("");
            Console.Write("Sum of all elements stored in the array is : {0}\n\n", sum);
            Console.Write("The average of all elements is : " + average);
            Console.WriteLine("");
            Console.ReadLine();
            Console.WriteLine("Do you want to enter another number of elements to be stored in the array (Yes/No)? : " );
            Console.ReadLine();

            
                
            }
        }

    }


What I have tried:

i have tried using if else statement and do while statement but the result is error
Posted
Updated 17-Oct-20 14:09pm
Comments
BillWoodruff 17-Oct-20 12:49pm
   
where is the error occurring ?

this is doing nothing:

for (i = 0; i < 2; i++)
{
yes = i;
}
husna bieber 17-Oct-20 12:58pm
   
sorry wrong word. i mean it doesn't gave result. before this i have tries if else statement. this is my coding.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab_Week6
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[100];
int i, n, sum = 0;
int average = 0;
int no = 1;
int yes = 0;



Console.Write("\n\nRead and Print elements of an array:\n");
Console.Write("-----------------------------------------\n");

Console.Write("Input the number of elements to be stored in the array :");
n = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}

for (i = 0; i < n; i++)
{
sum += arr[i];
}
average = sum / n;
Console.WriteLine("");
Console.Write("Sum of all elements stored in the array is : {0}\n\n", sum);
Console.Write("The average of all elements is : " + average);
Console.WriteLine("");
Console.ReadLine();
Console.WriteLine("Do yo want to enter another number of elements to be stored in the array (Yes/No)? : ");
Console.ReadLine();

if (yes == 0)
{
Console.Write("\n\nRead and Print elements of an array:\n");
Console.Write("-----------------------------------------\n");

Console.Write("Input the number of elements to be stored in the array :");
n = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}

for (i = 0; i < n; i++)
{
sum += arr[i];
}
average = sum / n;
Console.WriteLine("");
Console.Write("Sum of all elements stored in the array is : {0}\n\n", sum);
Console.Write("The average of all elements is : " + average);
Console.ReadLine();
}
else
{

Environment.Exit(no);
}

}
}

}

Rate this:
Please Sign up or sign in to vote.

Solution 1

You need to set up a loop around your existing code:
string reply = "";
do
   {
   ...your code here ...
   Console.Write("Do you want to exit now? (Y/N)");
   reply = Console.ReadLine().Trim().ToUpper();
   } while (string.IsNullOrWhitespace(reply) || reply[0] != 'N');
   
Comments
George Swan 17-Oct-20 19:22pm
   
Linq has an easy way to calculate the sum and average of your array
int sum = arr.Sum();
double average = arr.Average();
OriginalGriff 18-Oct-20 2:04am
   
If he can't write a loop yet, Linq is probably a bit advanced for him ... :D
George Swan 18-Oct-20 3:35am
   
Yes but the 'usings' included System.Linq so I thought I'd throw it in.
husna bieber 18-Oct-20 1:03am
   
Thank you so much OriginalGriff! It's working, i'm so grateful!
OriginalGriff 18-Oct-20 2:03am
   
You're welcome!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
i mean it doesn't gave result.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100