Venciity

[Jumpstart C#] ArraysAndLists TASK15

Feb 24th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. /*Задача М1-15.
  2.  * Установете поредния номер на първия и броя на всички от­ри­ца­тел­ни елементи на ма­сива А(N).*/
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace task15
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Write("Enter array length: ");
  17.             int length = int.Parse(Console.ReadLine());
  18.             int[] myArray = new int[length];
  19.  
  20.             //enter array elements
  21.             for (int index = 0; index < myArray.Length; index++)
  22.             {
  23.                 Console.Write("Number {0} = ", index + 1);
  24.                 myArray[index] = int.Parse(Console.ReadLine());
  25.             }
  26.  
  27.             int i = 0;
  28.             while (i < myArray.Length && myArray[i] >= 0)
  29.             {
  30.                 i++;
  31.             }
  32.             if (i == myArray.Length)
  33.             {
  34.                 Console.WriteLine("there is no negative elements int the array");
  35.             }
  36.             else
  37.             {
  38.                 int count = 1;
  39.                 for (int j = i+1; j < myArray.Length; j++)
  40.                 {
  41.                     if (myArray[j] < 0)
  42.                     {
  43.                         count++;
  44.                     }
  45.                 }
  46.                 Console.WriteLine("The index of first negative element is {0} and " +
  47.                     "the total number of all negative numbers is {1}",i,count);
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment