Irregular table Issue - c#

I wonder what I do wrong about that task:
I had to create a table[][,] where I enter from the keyboard number of groups, the number of students in each group and how many subjects they have.
For each student, I had to enter a grade for each subject.
I don't actually where is my mistake.
Because the program I wrote can't show properly full table (f.e. if in a group no. 1 I have 3 students and for a group no. 2 I have 1 student it shows for each group 1 student)
Also I used "float" to calculate the Average of all grades for each group but it shows the integer number.
Can you help me?
I attached the code down below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SpecialTask
{
class Program
{
static int[][,] tab1;
static int iSub = 0;
static int iGro = 0;
static int iStu = 0;
static int note = 0;
static int iSum = 0;
static float fAvg = 0.0f;
static int Subjects(string sSubjects)
{
do
{
Console.WriteLine("Please enter a number of subjects (2-4):");
}
while ((!int.TryParse(Console.ReadLine(), out iSub)) || (iSub < 2) || (iSub > 4));
return iSub;
}
static int Groups(string sGroups)
{
do
{
Console.WriteLine("Please enter a number of groups (1-3):");
}
while ((!int.TryParse(Console.ReadLine(), out iGro)) || (iGro < 1) || (iGro > 3));
return iGro;
}
static int Students(string sStudents)
{
do
{
Console.WriteLine("Please enter a number of students(1-5): ");
}
while ((int.TryParse(Console.ReadLine(), out iStu) == false) || (iStu < 1) || (iStu > 5));
return iStu;
}
static int Notes(string sNotes)
{
do
{
Console.WriteLine("Please enter a note (2-5): ");
}
while ((!int.TryParse(Console.ReadLine(), out note)) || (note < 2) || (note > 5));
return note;
}
static void TabData()
{
int i, j, k;
Subjects("Please enter a number of subjects (2-4):");
Groups("Please enter a number of groups (1-3):");
tab1 = new int[iGro][,];
for (i = 0; i < iGro; i++)
{
Console.WriteLine("Group NO.{0}", 1 + i);
Students("Please enter a number of students(1-5): ");
tab1[i] = new int[iStu, iSub];
for (j = 0; j < iStu; j++)
{
for (k = 0; k < iSub; k++)
{
Console.WriteLine("Student NO.{0}", 1 + j);
Console.WriteLine("Subject NO.{0}", 1 + k);
tab1[i][j, k] = Notes("Please enter a note (2-5): ");
}
}
}
Console.WriteLine("\n");
Console.WriteLine("\n");
for (i = 0; i < iGro; i++)
{
Console.WriteLine("Group NO.{0}: ", 1 + i);
for (j = 0; j < tab1.Length; j++)
{
Console.WriteLine("Student NO.{0}/Group NO.{1}: ",1+ j, 1 + i);
for (k = 0; k < iSub; k++)
{
Console.Write("Subject NO.{0}: ", 1 + k);
Console.Write(tab1[i][j, k]);
Console.Write("\n");
}
Console.WriteLine("\n");
}
}
}
static void avgG()
{
int i, j, k;
TabData();
for (i = 0; i < iGro; i++)
{
for (j = 0; j < iStu; j++)
{
for (k = 0; k < iSub; k++)
{
iSum = iSum + tab1[i][j, k];
}
}
fAvg = iSum / iSub;
Console.WriteLine("Sum: " + iSum);
Console.WriteLine("Average of all grades for Group NO. {0}: {1}", i, fAvg);
}
}
static void Main(string[] args)
{
avgG();
Console.ReadKey();
Console.ReadKey();
}
}
}

Related

I need to use the matrix array in the multiplyMatrixByConstant method but im not sure how

I'm getting an error for multiplyMatrixByConstant(); saying " there is no argument given that corresponds to the required formal parameter" I'm not sure what to include to allow me to use the matrix array in the multiplyMatrixByConstant(); method
public class Program
{
public static void Main()
{
Console.WriteLine(" ----------Welcome to the Matrix Program----------");
Console.WriteLine("Please select one of the following options:");
Console.WriteLine("1: The Random Matrix");
Console.WriteLine("2: The Transpose Matrix");
Console.WriteLine("3: Multiplying a Matrix by a Constant");
Console.WriteLine("4: Multiplying Two Matrices");
Console.Write("Your choice is: ");
string choice = Console.ReadLine();
if (choice == "1")
{
generateMatrix();
}
else if (choice == "2")
{
generateTranspose();
}
else if (choice == "3")
{
multiplyMatrixByConstant();
}
}
static int[, ] generateMatrix()
{
Console.Write("Enter the number of columns: ");
int c = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of rows: ");
int r = Convert.ToInt32(Console.ReadLine());
int[, ] matrix = new int[c, r];
Random rnd = new Random();
Console.WriteLine("The Matrix is: ");
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
matrix[i, x] = rnd.Next(0, 10);
}
}
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
Console.Write(matrix[i, x] + " ");
}
Console.WriteLine(" ");
}
return (matrix);
}
static void generateTranspose()
{
Console.Write("Enter the number of columns: ");
int c = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of rows: ");
int r = Convert.ToInt32(Console.ReadLine());
int[, ] matrix = new int[c, r];
Random rnd = new Random();
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
matrix[i, x] = rnd.Next(0, 10);
}
}
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
Console.Write(matrix[i, x] + " ");
}
Console.WriteLine(" ");
}
Console.WriteLine("The Transpose is:");
int[, ] transpose = new int[r, c];
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
transpose[x, i] = matrix[i, x];
}
}
for (int i = 0; i < r; i++)
{
for (int x = 0; x < c; x++)
{
Console.Write(transpose[i, x] + " ");
}
Console.WriteLine(" ");
}
}
static void multiplyMatrixByConstant(int[, ] matrix, int c, int r)
{
generateMatrix();
Console.Write(" Enter a Constant to Multiply the Matrix by: ");
int constant = Convert.ToInt32(Console.ReadLine());
int[, ] result = new int[c, r];
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
result[i, x] = matrix[i, x] * constant;
}
}
for (int i = 0; i < c; i++)
{
for (int x = 0; x < r; x++)
{
Console.Write(result[i, x] + " ");
}
Console.WriteLine(" ");
}
}
}
You need to income parameters, (int[, ] matrix, int c and int r).

Magic Square Code Single Even number in C#

can you help me to create a logic for magic square metric. In given example, I have created a code for generate Magic Square for odd numbers like 3x3, 5x5, 7x7 metric and double even numbers like 4×4 , 8×8 but unable to found a proper solution for create single even value magic square metric like 6x6, 10x10 etc.
In current implementation anyone can enter a number (n) in input and it will create a nxn magic square metric. But not working fine with single even numbers
class Program
{
public static void Main(string [] args )
{
Console.WriteLine("Please enter a number:");
int n1 = int.Parse(Console.ReadLine());
// int[,] matrix = new int[n1, n1];
if (n1 <= 0)
{
Negativ();
}
else if (n1 == 2)
{
Zwei();
}
else if ((n1 != 2) && !(n1 < 0) && (n1 % 2 != 0))
{
Odd (n1 );
}
else if ((n1 != 2) && !(n1 < 0) && ((n1 - 2) % 4 == 0))
{//singl Even
SingleEven(n1);
}
else if ((n1 != 2) && !(n1 < 0) && (n1 % 4 == 0))
{
DoubleEven (n1);
}
}
private static void Negativ(){
Console.WriteLine("Sorry, the number must be positive and greater than 3 ");
Console.ReadLine();
}
public static void Zwei(){
Console.WriteLine("Sorry,there is no magic square of 2x2 and the number must be and greater than 3 ");
Console.ReadLine();
}
public static void Odd ( int n)// odd method
{
int[,] magicSquareOdd = new int[n, n];
int i;
int j;
// Initialize position for 1
i = n / 2;
j = n - 1;
// One by one put all values in magic square
for (int num = 1; num <= n * n; )
{
if (i == -1 && j == n) //3rd condition
{
j = n - 2;
i = 0;
}
else
{
//1st condition helper if next number
// goes to out of square's right side
if (j == n)
j = 0;
//1st condition helper if next number is
// goes to out of square's upper side
if (i < 0)
i = n - 1;
}
//2nd condition
if (magicSquareOdd[i, j] != 0)
{
j -= 2;
i++;
continue;
}
else
{
//set number
magicSquareOdd[i, j] = num++;
//1st condition
j++; i--;
}
}
// print magic square
Console.WriteLine("The Magic Square for " + n + " is : ");
Console.ReadLine();
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
Console.Write(" " + magicSquareOdd[i, j] + " ");
Console.WriteLine();
Console.ReadLine();
}
Console.WriteLine(" The sum of each row or column is : " + n * (n * n + 1) / 2 + "");
Console.ReadLine();
}
public static void SingleEven(int n )
{
// int n = magic .Length ;
int[,] magicSquareSingleEven = new int[n, n];
int halfN = n / 2;
int k = (n - 2) / 4;
int temp;
int[] swapcol = new int[n];
int index = 0;
int[,] minimagic = new int[halfN, halfN];
*Odd(minimagic) ;* // here is the problem
for (int i = 0; i < halfN; i++)
for (int j = 0; j < halfN; j++)
{
magicSquareSingleEven[i, j] = minimagic[i, j];
magicSquareSingleEven[i+ halfN , j+halfN ] = minimagic[i, j]+ halfN *halfN ;
magicSquareSingleEven[i, j + halfN] = minimagic[i, j] +2* halfN * halfN;
magicSquareSingleEven[i + halfN, j] = minimagic[i, j] +3* halfN * halfN;
}
for (int i =1; i< k ;i ++)
swapcol [index ++]=i ;
for (int i = n-k+2; i <= n ; i++)
swapcol[index++] = i;
for (int i =1; i<=halfN ;i ++)
for (int j = 1; j<= index ; j ++)
{
temp = magicSquareSingleEven[i - 1, swapcol[j - 1] - 1];
magicSquareSingleEven[i-1,swapcol[j-1]-1]=magicSquareSingleEven[i +halfN-1,swapcol[j-1]-1];
magicSquareSingleEven[i+halfN-1,swapcol[j-1]-1]=temp;
}
//swaping noses
temp=magicSquareSingleEven[k,0];
magicSquareSingleEven[k,0]=magicSquareSingleEven[k+halfN,0];
magicSquareSingleEven[k+halfN,0]=temp;
temp=magicSquareSingleEven[k+halfN,k];
magicSquareSingleEven[k+halfN,k]=magicSquareSingleEven[k,k];
magicSquareSingleEven[k,k]=temp;}
//end of swaping
// print magic square
Console.WriteLine("The Magic Square for " + n + " is : ");
Console.ReadLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write(" " + magicSquareSingleEven[i, j] + " ");
Console.WriteLine();
Console.ReadLine();
}
Console.WriteLine(" The sum of each row or column is : " + n * (n * n + 1) / 2 + "");
Console.ReadLine();
}

How do you find averages of numbers in multidimensional arrays from user input?

This is my first attempt at creating a multidimensional array. I have the user enter the number of students in a class(the rows), and then have them enter the number of scores they will input(the columns). I now want to add all the scores of each individual student and find each of their grade averages. I can't figure out how to separate out the information for each individual student's data. Here is what I have so far:
public static void Main(string[] args)
{
int TotalStudents = 0;
int TotalGrades = 0;
int sum = 0;
Console.WriteLine("Enter the number of students: ");
TotalStudents = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the number of grades: ");
TotalGrades = Convert.ToInt32(Console.ReadLine());
int[,] scoresArray = new int[TotalStudents, TotalGrades];
for (int i = 0; i < TotalStudents; i++)
for (int j = 0; j < TotalGrades; j++)
{
Console.Write("Please enter score {0} for student {1}:", j + 1, i + 1);
scoresArray[i, j] = Convert.ToInt32(Console.ReadLine());
sum = sum + Convert.ToInt32(scoresArray[i,j]);
}
double gradePercent = sum / (TotalGrades * 100);
double gradePer100 = gradePercent * 100;
string gradeLetter = "";
if (gradePer100 >= 90)
{
gradeLetter = "A";
}
else if (gradePer100 >= 80 && gradePer100 < 90)
{
gradeLetter = "B";
}
else if (gradePer100 >= 70 && gradePer100 < 80)
{
gradeLetter = "C";
}
else if (gradePer100 >= 60 && gradePer100 < 70)
{
gradeLetter = "D";
}
else
{
gradeLetter = "F";
}
Console.WriteLine("\nStudent average score is: " + gradePer100);
Console.WriteLine("\nStudent will recieve a " + gradeLetter + " in the class.");
Console.Write("\nPress the [ENTER] key to exit.");
Console.ReadLine();
}
You can create a 2-dimensional array like this :
var array = new int[TotalStudents, TotalGrades];
And then fill it:
for(int i = 0; i < TotalStudents; i++)
for(int j = 0; j < TotalGrades; j++)
{
Console.Write("Please enter score {0} for student {1}", j + 1, i + 1);
array[i, j] = Convert.ToInt32(Console.ReadLine());
}
If the scores are doubles just change new int[TotalStudents, TotalGrades] to new double[TotalStudents, TotalGrades] and Convert.ToInt32(Console.ReadLine()) to Convert.ToDouble(Console.ReadLine())

Count No Of 1's Colony From [1,0] 2D Array Using C#

I have a 2D array of integers which stores only values [1,0] as shown below.
int[,] INPUT = new int[10, 10] {
{1,1,0,0,0,1,1,1,0,0},
{1,0,0,0,1,0,0,0,1,0},
{0,0,0,1,1,0,0,1,0,1},
{1,1,0,0,0,0,0,1,1,0},
{0,1,0,0,0,1,0,1,1,0},
{0,0,0,0,1,0,1,0,0,0},
{1,0,0,1,0,0,1,1,1,0},
{0,1,0,0,1,1,0,0,1,1},
{0,1,1,1,0,1,0,1,1,0},
{0,0,0,0,0,1,1,0,0,0},
};
I need to identify a Colony count for the input image, where a Colony is defined as a contiguous sequence of 1s (Like in this array, there are 3 colony of 1's). Connection between 1s can be either adjacent or on the diagonal.
I am required to use recursion for this.
class Program
{
public static int count;
public static int colony;
public static int[,] INPUT;
public static int rows = 10;
public static int cols = 10;
static void Main(string[] args)
{
int[,] INPUT = new int[10, 10] {
{1,1,0,0,0,1,1,1,0,0},
{1,0,0,0,1,0,0,0,1,0},
{0,0,0,1,1,0,0,1,0,1},
{1,1,0,0,0,0,0,1,1,0},
{0,1,0,0,0,1,0,1,1,0},
{0,0,0,0,1,0,1,0,0,0},
{1,0,0,1,0,0,1,1,1,0},
{0,1,0,0,1,1,0,0,1,1},
{0,1,1,1,0,1,0,1,1,0},
{0,0,0,0,0,1,1,0,0,0},
};
// Showing All Values From Upper 2D array
Console.WriteLine("Input File Contain: \n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(INPUT[i, j]);
}
Console.WriteLine();
}
// Getting Total 1's Count Only
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (INPUT[i, j] == 1)
{
count = count + 1;
}
}
Console.WriteLine();
}
// Getting Total 1's Colony Only
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (INPUT[i, j] == 1)
{
INPUT[i, j] = 0;
colony = GetColony(i, j);
}
}
}
Console.WriteLine("Counts Of 1 Are: " + count);
Console.WriteLine("Counts Of 1's Colony Are: " + colony );
}
static public int GetColony(int i, int j)
{
if ((i < rows) && (j < cols))
{
if ((INPUT[i - 1, j] == 1) || (INPUT[i + 1, j] == 1) || (INPUT[i, j - 1] == 1) || (INPUT[i, j + 1] == 1))
{
INPUT[i, j] = 0;
return GetColony(i, j);
}
else
{
return 1;
}
}
else
{
return 1;
}
}
}

Array Duplicate Elimination with c#?

I have a program here that need some improvements. This Program inputs 5 elements in an Array and Removes if any duplicates. It works but the problem is that it sets every duplicate to zero. I don't want to display zero. I want it completely destroyed and eliminated. I don't want that duplicate element to appear. This is what I have so Far! Could Use some help. Thank You.
// Gurpreet Singh
// Duplicate Program
using System;
class duplicate
{
static void Main()
{
const int Array_Size = 5;
int [] number = new int [Array_Size];
int i;
for ( i = 0; i < Array_Size; i++)
{
Console.Write("Element " + i + ": ");
number[i] = Int32.Parse(Console.ReadLine());
if (number[i] < 9 || number[i] > 101)
{
Console.WriteLine("Enter Number between 10 - 100");
number[i] = Int32.Parse(Console.ReadLine());
}
}
for (i = 0; i < Array_Size; i++)
{
for (int j = 0; j < Array_Size; j++)
{
if (i != j)
{
if (number[j] == number[i])
number[j] = 0;
}
}
}
Console.WriteLine("Duplicate Removed:");
for (i = 0; i < Array_Size; i++)
{
Console.WriteLine("Element " + i + " " + number[i]);
}
Console.ReadLine();
}
}
The easiest way is to use Linq's Distinct method:
number = number.Distinct().ToArray();
This will return a new array without any duplicates.
The duplicate is displayed as zero, since you assign the value of the duplicate to be zero, in the line,
if(number[j]==number[i])
number[j]=0
to delete the element from the array, use the following code:
if(number[j]==number[i])
{
int k=j;
while(k<Array_Size-1)
{
number[k]=number[k+1];
k++;
}
Array_Size--;
}
the statement Array_Size--; is done so that the last element is not repeated twice
This is my complete code in which I put some double-for-loop statement to
prevent it from inserting the duplicated integers in an array.
Have a look.
class Program
{
static void Main(string[] args)
{
const int ARRAY_SIZE = 5;
int[] ArrayTable = new int[ARRAY_SIZE];
int Element=0;
int a;
for(a=0; a<ArrayTable.Length;a++)
{
Console.Write("Please Enter an integer (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
while (Element < 10 || Element > 100)
{
Console.Write("Try again (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
}
ArrayTable[a] = Element;
for (int b = 0; b < a; b++)
{
while (ArrayTable[a] == ArrayTable[b])
{
Console.Write("Integer Duplicated!\nTry again: ");
Element = Int32.Parse(Console.ReadLine());
ArrayTable[a] = Element;
Console.WriteLine();
while (Element < 10 || Element > 100)
{
Console.Write("Try again (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
ArrayTable[a] = Element;
}
}
}
}
for (int c = 0; c < ArrayTable.Length; c++)
{
Console.Write("{0} ", ArrayTable[c]);
}
}

Categories