I must create code in C# to:
Ask the user to enter an arbitrary set of numbers into an array and display all the entered numbers.
I done this part and the code is working properly
Then multiply pairs of numbers together and display the result. If you have an odd number of numbers then just display the last number.
E.G.
2 3 8 4 become 6 32
2 3 8 4 7 become 6 32 7
My problem is with the odd arrays. if the arrays has even number of element it's no problem, but if the arrays has odd number of elements there are errors, never print the last element.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayDisplay
{
class Array
{
static void Main(string[] args)
{
int[] array = new int[9];
int userInput = -1;
int itt = 0;
int count = 0;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
while (userInput != -1 && itt<array.Length)
{
array[itt] = userInput;
itt++;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
}
Console.WriteLine("The array contains: ");
for (int i = 0; i < array.Length; i++) {
Console.Write(" {0} , " ,array[i]);
}
Console.WriteLine("");
Console.WriteLine("count {0}",count-1);
if (count % 2 == 0)
{
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
else {
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1])
}
}
}
}
}
Because 7*0=0, try below:
if (count % 2 == 0)
{
for (int i = 0; i < array.Length - 1; i += 2)
{
Console.Write(" {0} , ", i == count - 2 ? array[i] : array[i] * array[i + 1]);
}
}
else
{
for (int i = 0; i < array.Length - 1; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
Try this:
int[] array = new int[9];
int userInput = -1;
int itt = 0;
int count = 0;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
while (userInput != -1 && itt < array.Length)
{
array[itt] = userInput;
itt++;
if (count == 9) break;
Console.WriteLine("Enter a integer number or -1 to exit:");
userInput = Convert.ToInt32(Console.ReadLine());
count++;
}
Console.WriteLine("The array contains: ");
for (int i = 0; i < array.Length; i++)
{
Console.Write(" {0} , ", array[i]);
}
Console.WriteLine("");
Console.WriteLine("count {0}", count);
if (count % 2 == 0)
{
for (int i = 0; i < array.Length; i += 2)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
else
{
for (
int i = 0; i < array.Length; i += 1)
{
if (i == 7)
{
Console.Write(" {0} , ", array[i] * array[i + 1]);
break;
}
Console.Write(" {0} , ", array[i] * array[i + 1]);
}
}
Console.ReadLine()
;
Related
i want to write a console app that get int value from user and put them in a array and show sum of the numbers and min and max of them and then print them in the console in order
i write until this point of project but have some bugs...
Console.WriteLine("\n Please Enter the Number of your Numbers: \n");
int k = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Please Enter Your Numbers:");
int[] myArray = new int[k];
int sum = 0;
//INPUT
for (int i = 0; i < k; i++)
{
myArray[i] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Number [" + (i+1) + "]: " + myArray[i] + " and Next:");
}
//DELETE DUPLICATE ELEMENTS
int[] newArray = myArray.Distinct().ToArray();
//SORT
Array.Sort(newArray);
Console.WriteLine("\n Your Sorted Numbers Without Duplicated ones: ");
foreach (int i in newArray)
{
Console.Write(" | " + i);
}
Console.Write(" |");
i think you are trying to make a program like this:
int i,n;
int[] a = new int[100];
Console.WriteLine("element numbers :");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("please entre your numbers : ", n);
for (i = 0; i < n; i++)
{Console.WriteLine($"element i");
a[i] = Convert.ToInt32(Console.ReadLine());}
int sum = 0;
for (i = 0; i < n; i++)
{sum += a[i];}
Console.WriteLine("sum is : {0}", sum);
int max = a[0];
int min = a[0];
for (i = 1; i < n; i++)
{if (a[i] > max)
{max = a[i];}
if (a[i] < min)
{min = a[i];}}
Console.WriteLine("max is : " + max);
Console.WriteLine("min is : " + min);
Console.WriteLine("element numbers are: ");
for (i = 0; i < n; i++)
{Console.WriteLine(a[i]);}
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();
}
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();
}
}
}
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())
I wrote this code to order any set of numbers from biggest to smallest, but for whatever reason, the output always has a zero at the end. My question is, where did it come from? (new to coding)
Console.WriteLine("Please enter set of numbers");
int input = Convert.ToInt16(Console.ReadLine());
int counter= 1;
int temp1;
int temp2;
int[] array = new int[input+1];
for (int i = 0; i <=input-1; i++)
{
Console.WriteLine("Please enter entry number " + counter);
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
counter++;
}
for (int x = 0; x <= input; x++)
{
for (int i = 1; i <=input; i++)
{
if (array[i - 1] <= array[i])
{
temp1 = array[i - 1];
temp2 = array[i];
array[i - 1] = temp2;
array[i] = temp1;
}
}
}
Console.WriteLine();
for (int i = 0; i<=input; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadLine();
You're inconsistent between whether you're trying to handle input + 1 or input elements. For example:
int[] array = new int[input+1];
for (int i = 0; i <=input-1; i++)
{
Console.WriteLine("Please enter entry number " + counter);
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
counter++;
}
You're creating an array with input + 1 elements, but only populating input of them.
In general, it's much more common to use exclusive upper boundaries for loops. For example:
int[] array = new int[input];
for (int i = 0; i < input; i++)
{
// No need for the counter variable at all
Console.WriteLine("Please enter entry number " + (i + 1));
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
}