Advertisement
Guest User

week 2 task 9

a guest
Jan 16th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication6
  8. {
  9.     class Program
  10.     {
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             int[] newarray = new int[10];
  15.             int count = 0;
  16.  
  17.             Random rand = new Random();
  18.             while (count < 10)
  19.             {
  20.                 int num = rand.Next(10) + 1;
  21.  
  22.                 newarray[count] = num;
  23.                 count++;
  24.             }
  25.  
  26.             //creates array
  27.  
  28.             Console.WriteLine("Printing array...");
  29.             count = 0;
  30.             while (count < newarray.Length)
  31.             {
  32.                 Console.WriteLine(newarray[count]);
  33.                 count++;
  34.             }
  35.             Console.WriteLine("Done!\n\n");
  36.             halfcomparer(newarray);
  37.         }
  38.  
  39.  
  40.  
  41.  
  42.         static void halfcomparer(int[] array)
  43.         {
  44.             int sum1 = 0, sum2 = 0;
  45.  
  46.             int count = 0;
  47.             while (count < (array.Length / 2))
  48.             {
  49.                 sum1 = sum1 + array[count];
  50.                 count++;
  51.             }
  52.             Console.WriteLine("The first half's total is: " + sum1);
  53.  
  54.             count = 0;
  55.             while (count < array.Length)
  56.             {
  57.                 sum2 = sum2 + array[count];
  58.                 count++;
  59.             }
  60.             sum2 = sum2 - sum1;
  61.  
  62.             Console.WriteLine("The second half's total is: " + sum2);
  63.  
  64.             if (sum1 > sum2)
  65.             {
  66.                 Console.WriteLine("The first half's sum is bigger");
  67.             }
  68.  
  69.             else if (sum1 < sum2)
  70.             {
  71.                 Console.WriteLine("The second half's sum is bigger");
  72.             }
  73.  
  74.             else if (sum1 == sum2)
  75.             {
  76.                 Console.WriteLine("Both halves are equal");
  77.             }
  78.             Console.ReadKey();
  79.             ;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement