Advertisement
Guest User

Question2

a guest
Feb 28th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5.     public class Question2
  6.     {
  7.         /*
  8.         static void Main(string[] args)
  9.         {
  10.             int length = 0, i = 0;
  11.  
  12.             while (length <= 0) // Making sure the length isn't negative (!)
  13.             {
  14.                 Console.WriteLine("Enter array's length: ");
  15.                 length = int.Parse(Console.ReadLine());
  16.             }
  17.  
  18.             int[] arr = new int[length];
  19.            
  20.             // Scanning values into the array:
  21.  
  22.             for (i = 0; i < arr.Length; i++)
  23.             {
  24.                 Console.Write("Enter a number: ");
  25.                 arr[i] = int.Parse(Console.ReadLine());
  26.             }
  27.            
  28.             // Printing the results: which sum is bigger (?)
  29.  
  30.             if (IsEvenSumBigger(arr) == 1)
  31.             {
  32.                 Console.WriteLine("Sum of evens is bigger than sum of odds!");
  33.             }
  34.             else if(IsEvenSumBigger(arr) == -1)
  35.             {
  36.                 Console.WriteLine("Sum of evens is smaller than sum of odds!");
  37.             }
  38.             else
  39.             {
  40.                 Console.WriteLine("Sum of evens and odds are equal!");
  41.             }
  42.         }
  43. */
  44.         /*
  45.             A function that checks whether the sum of the numbers in the even indexes are bigger.
  46.            
  47.             Input: arr - The array we check.
  48.             Output: 1 - Even sum bigger, 0 - Sums are equal, -1 - Odds are bigger.
  49.          */
  50.        
  51.         public static int IsEvenSumBigger(int[] arr)
  52.         {
  53.             int i = 0, sumOfOdds = 0, sumOfEvens = 0, returnAnswer = 0;
  54.             for (i = 0; i < arr.Length; i++)
  55.             {
  56.                 if (i % 2 == 0) // In other words, an even index.
  57.                 {
  58.                     sumOfEvens += arr[i];
  59.                 }
  60.                 else
  61.                 {
  62.                     sumOfOdds += arr[i];
  63.                 }
  64.             }
  65.            
  66.             // Avoid multiple return statments:
  67.  
  68.             if (sumOfOdds > sumOfEvens)
  69.             {
  70.                 returnAnswer = -1;
  71.             }
  72.             else if (sumOfEvens > sumOfOdds)
  73.             {
  74.                 returnAnswer = 1;
  75.             }
  76.             else
  77.             {
  78.                 returnAnswer = 0;
  79.             }
  80.  
  81.             return returnAnswer;
  82.  
  83.         }
  84.        
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement