Midterm 1

CSharp · Console

/Program.cs

// The element x[k] of integer sequence x0..xn is called “achiever” if it exceeds any of
// preceding elements (for any i<k : x[i] < x[k]). Find inductive extension of the function
// “number of achievers in a sequence of integers” (please write it explicitly in plain words!). 
// Write a program that inputs any number of non-zero integers followed by 0 and
// prints total number of achievers (last 0 is not counted).
using System;
					
public class Program
{
	public static int InputInt(string prompt)
	{
		Console.Write("Enter " + prompt + ": ");
		return Convert.ToInt32(Console.ReadLine());
	}
	
	public static void Main(string[] args)
	{
		int nextInt = InputInt("Next Integer");

		// we should either process zero length separately or
		// introduce pseudo-element (that's what we'll do)
		
		// Inductive extension:		
		int achiever = 0;		// number of achievers so far
		int max = nextInt-1;	// max element so far
		
		while (nextInt != 0)
		{
			if (nextInt > max)
			{
				achiever++;
				max = nextInt;
			}
			nextInt = InputInt("Next Integer");
		}
		Console.WriteLine(achiever);
	}
}
;