Hi to everyone,
I'm newbie here and in C# also. I'd like to give you congratulations about this forum!
I'd like also to ask you how to insert values in an Array so i can create a dynamic array...
My problem is to make a program to give my grades and return the sum of them!
Source code is provided:
Any help please???
Thanx in advance...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Courses
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Give me the number of courses you have passed:");
String a = Console.ReadLine();
int b = Convert.ToInt16(a);
int sum = 0;
if ((b <= 0) || (b > 20))
{
Console.WriteLine("You gave me wrong number of courses,try again!!!");
}
else
{
int i = 1;
while (i <= b)
{
Console.WriteLine("Give me the grades of your courses,pressing Enter: " + i);
i++;
}
int[] num = new int[20];
for (int j = 1; j < i; j++)
{
string source = Console.ReadLine();
int pin = num[j];
sum = sum + num[j];
}
Console.WriteLine("The suma is: " + sum);
Console.ReadKey();
}
}
}
}
Loading
theLizardPosted Mar 26, 2010, 4:55 PM
This code works fine, Maybe has nothing to do with it....
int[] num = new int[0];
Console.WriteLine("Give me the number of courses you have passed:");
int b = Convert.ToInt16(Console.ReadLine());
int sum = 0;
int i = 1;
double avg = 0;
while (i <= b)
{
Console.WriteLine("Give me the grades of your courses,pressing Enter: " + i);
string source = Console.ReadLine();
Array.Resize(ref num, num.Length + 1);
num[num.Length-1] = int.Parse(source);
sum += num[num.Length-1];
i++;
}
avg = sum / b; //this gives integers only, NO decimal point values
avg = Convert.ToDouble(sum) / Convert.ToDouble(b); //this will give a true average with decimal point...
Console.WriteLine("The suma is: " + sum);
Console.WriteLine("The average value of your grades is: " + avg);
Console.ReadKey();
added extra 3 lines of code removed 1,
which code is more efficient ? 4 lines less code, one while loop, NO for loop and dynamic array.
Christos AlexiouPosted Mar 27, 2010, 6:13 AM
The only thing that is needed to this code is the declaration of avg variable.
Type declaration of this variable is double avg not int of course!!!
Jitendra TiwariPosted Mar 27, 2010, 6:09 AM
to calculate avarage just put a line of code
avg=sum/b;
console.WriteLine("Avarage :"+avg);
Christos AlexiouPosted Mar 26, 2010, 5:24 PM
Thank you!!!
Christos AlexiouPosted Mar 26, 2010, 8:52 AM
Take a look to my last code...
Jitendra TiwariPosted Mar 26, 2010, 8:48 AM
use this code instead of ur code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Courses
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Give me the number of courses you have passed:");
String a = Console.ReadLine();
int b = Convert.ToInt16(a);
int sum = 0;
if ((b <= 0) || (b > 20))
{
Console.WriteLine("You gave me wrong number of courses,try again!!!");
}
else
{
int[] num = new int[20];
int i = 1;
while (i <= b)
{
Console.WriteLine("Give me the grades of your courses,pressing Enter: " + i);
string str = Console.ReadLine();
num[i] = Convert.ToInt16(str);
i++;
}
foreach (int n in num)
{
int pin = n;
sum = sum + n;
}
//for (int j = 1; j < i; j++)
//{
// string source = Console.ReadLine();
// int pin = num[j];
// sum = sum + num[j];
//}
Console.WriteLine("The suma is: " + sum);
Console.ReadKey();
}
}
}
}
if u like answer please mark this...
Christos AlexiouPosted Mar 26, 2010, 7:53 AM
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Courses
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Give me the number of courses you have passed:");
String a = Console.ReadLine(); // The given numbers of courses
int b = Convert.ToInt32(a); // Convert the string to int
double avg = 0;
if ((b <= 0) || (b > 20)) //Check if the number of courses are out of limits
{
Console.WriteLine("You gave me wrong number of courses,try again!!!");
}
else
{
int i = 1;
while (i <= b)
{
Console.WriteLine("Give me the grades of your courses,pressing Enter: " + i);
i++;
}
for (int j = 1; j < i; j++) {
int[] num = new int[20]; //Define array of 20 points
num[j] = int.Parse(Console.ReadLine()); // Store the given grades to the array
avg += num[j];
}
avg = avg / b; //Calculation of average
Console.WriteLine("The average value of your grades is: " + avg);
Console.ReadKey();
}
}
}
}
Christos AlexiouPosted Mar 26, 2010, 4:15 AM
Thanx for your try...
theLizardPosted Mar 26, 2010, 4:07 AM
Console.WriteLine("Give me the number of courses you have passed:");
String a = Console.ReadLine();
int b = Convert.ToInt16(a);
int sum = 0;
int i = 1;
int[] num = new int[0];
Console.WriteLine("Give me the grades of your courses,pressing Enter: " + i);
while (i <= b)
{
string source = Console.ReadLine();
Array.Resize(ref num, num.Length + 1);
num[num.Length-1] = int.Parse(source);
sum += num[num.Length-1];
i++;
}
Console.WriteLine("The suma is: " + sum);
Console.ReadKey();
Christos AlexiouPosted Mar 26, 2010, 3:41 AM
That's the solution!!!
abhay pundirPosted Mar 26, 2010, 3:36 AM
static void Main(string[] args)
{
Console.WriteLine("Give me the number of courses you have passed:");
String a = Console.ReadLine();
int b = Convert.ToInt16(a);
int sum = 0;
if ((b <= 0) || (b > 20))
{
Console.WriteLine("You gave me wrong number of courses,try again!!!");
}
else
{
int i = 1;
while (i <= b)
{
Console.WriteLine("Give me the grades of your courses,pressing Enter: " + i);
i++;
}
int[] num = new int[20];
for (int j = 1; j < i; j++)
{
num[j] =int.Parse (Console.ReadLine());
sum = sum + num[j];
}
Console.WriteLine("The suma is: " + sum);
Console.ReadKey();
}
}
Christos AlexiouPosted Mar 26, 2010, 3:13 AM
The problem in the code is how to insert the given values from keyboard
in an array so I can take the sum of them with a for loop...
theLizardPosted Mar 25, 2010, 5:49 PM
int[] num = new int[0];
while (i <= b)
{
Console.WriteLine("Give me the grades of your courses,pressing Enter: " + i);
string source = Console.ReadLine();
Array.Resize(ref num, num.Length+1)
num[num.Length-1] = int.Parse(source);
sum = sum + num[num.Length-1];
i++;
}
something along these lines I have not tested but should work.
Christos AlexiouPosted Mar 25, 2010, 12:09 PM
Christos AlexiouPosted Mar 25, 2010, 12:07 PM
BuntyPosted Mar 25, 2010, 12:04 PM
http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingWithArrays11142005060354AM/WorkingWithArrays.aspx
Brief Description
Programming C# is a new self-taught series of articles, in which I will teach you C# language in a tutorial format. This article concentrates on arrays in .NET and how you can work with arrays using C# language. Article also covers the Arrays class and its method, which can be used to sort, search, get, and set an array items.
Introduction
In C#, an array index starts at zero. That means first item of an array will be stored at 0th position. The position of the last item on an array will total number of items - 1.
In C#, arrays can be declared as fixed length or dynamic. Fixed length array can stores a predefined number of items, while size of dynamic arrays increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined. For example, the following like declares a dynamic array of integers.
int [] intArray;
The following code declares an array, which can store 5 items starting from index 0 to 4.
int [] intArray;
intArray = new int[5];
The following code declares an array that can store 100 items starting from index 0 to 99.
int [] intArray;
intArray = new int[100];
Single Dimension Arrays
Arrays can be divided into four categories. These categories are single-dimensional arrays, multidimensional arrays or rectangular arrays, jagged arrays, and mixed arrays.
Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored in a row starting from 0 to the size of array - 1.
In C# arrays are objects. That means declaring an array doesn't create an array. After declaring an array, you need to instantiate an array by using the "new" operator.
The following code declares a integer array, which can store 3 items. As you can see from the code, first I declare the array using [] bracket and after that I instantiate the array by calling new operator.
int [] intArray;
intArray = new int[3];
Array declarations in C# are pretty simple. You put array items in curly braces ({}). If an array is not initialized, its items are automatically initialized to the default initial value for the array type if the array is not initialized at the time it is declared.
The following code declares and initializes an array of three items of integer type.
int [] intArray;
intArray = new int[3] {0, 1, 2};
The following code declares and initializes an array of 5 string items.
string[] strArray = new string[5] {"Ronnie", "Jack", "Lori", "Max", "Tricky"};
You can even direct assign these values without using the new operator.
string[] strArray = {"Ronnie", "Jack", "Lori", "Max", "Tricky"};
Multi Dimension Arrays
A multidimensional array is an array with more than one dimension. A multi dimension array is declared as following:
string[,] strArray;
After declaring an array, you can specify the size of array dimensions if you want a fixed size array or dynamic arrays. For example, the following code two examples create two multi dimension arrays with a matrix of 3x2 and 2x2. The first array can store 6 items and second array can store 4 items respectively.
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] names = new string[2, 2] { {"Rosy","Amy"}, {"Peter","Albert"} };
If you don't want to specify the size of arrays, just don't define a number when you call new operator. For example,
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] names = new string[,] { {"Rosy","Amy"}, {"Peter","Albert"} };
You can also omit the new operator as we did in single dimension arrays. You can assign these values directly without using the new operator. For example:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Rosy", "Amy"}, {"Peter", "Albert"} };
Jagged Arrays
Jagged arrays are often called array of arrays. An element of a jagged array itself is an array. For example, you can define an array of names of students of a class where a name itself can be an array of three strings - first name, middle name and last name. Another example of jagged arrays is an array of integers containing another array of integers. For example,
int[][] numArray = new int[][] { new int[] {1,3,5}, new int[] {2,4,6,8,10} };
Again, you can specify the size when you call the new operator.
Mixed Arrays
Mixed arrays are a combination of multi-dimension arrays and jagged arrays. Multi-dimension arrays are also called as rectangular arrays.
Accessing Arrays using foreach Loop
The foreach control statement (loop) of C# is a new to C++ or other
int[] numArray = {1, 3, 5, 7, 9, 11, 13};
foreach (int num in numArray)
{
System.Console.WriteLine(num.ToString());
}
A Simple Example
This sample code listed in Listing 1 shows you how to use arrays. You can access an array items by using for loop but using foreach loop is easy to use and better.
Listing 1. Using arrays in C#.
using System;
namespace ArraysSamp
{
class Class1
{
static void Main(string[] args)
{
int[] intArray = new int[3];
intArray[0] = 3;
intArray[1] = 6;
intArray[2] = 9;
Console.WriteLine("================");
foreach (int i in intArray)
{
Console.WriteLine(i.ToString() );
}
string[] strArray = new string[5]
{"Ronnie", "Jack", "Lori", "Max", "Tricky"};
Console.WriteLine("================");
foreach( string str in strArray)
{
Console.WriteLine(str);
}
Console.WriteLine("================");
string[,] names = new string[,]
{
{"Rosy","Amy"},
{"Peter","Albert"}
};
foreach( string str in names)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
}