Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //6. Write a program that reads two integer numbers N and K and an array of N elements from the console.
- //Find in the array those K elements that have maximal sum.
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter the N value :");
- int N = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter the K value :");
- int K = int.Parse(Console.ReadLine());
- int[] arr = new int[N];
- //Entering the values of each item in the array
- for (int i = 0; i < N; i++)
- {
- Console.Write("array[{0}] = ", i);
- arr[i] = int.Parse(Console.ReadLine());
- }
- //Πrdering the values in the array descendingly using the bubble method
- int aux = 0;
- for (int a = 0; a < N; a++)
- {
- for (int b = 0; b < N - 1; b++)
- {
- if (arr[b] < arr[b + 1])
- {
- aux = arr[b];
- arr[b] = arr[b + 1];
- arr[b + 1] = aux;
- }
- }
- }
- Console.Write("The {0} elements that have maximal sum in the array are: ", K);
- for (int i = 0; i < K; i++)
- {
- Console.Write("{0}", arr[i]);
- if (i != K - 1)
- {
- Console.Write(", ");
- }
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement