Задания 4 и 5:
Задание 4:
static void Main(string[] args)
{
int N = 10, count, sum = 0;
int[] mas = new int[N];
Random rnd = new Random();
for (int i = 0; i < N; i++)
{
mas[i] = rnd.Next(0, 10);
}
Console.WriteLine("Массив: ");
for (int i = 0; i < N; i++)
{
Console.Write(String.Format("{0,4}", mas[i]));
}
Console.WriteLine();
Console.WriteLine();
Console.Write("Введите количество элементов для суммирования (n): ");
count = Convert.ToInt32(Console.ReadLine());
if (count > N)
{
count = N;
Console.WriteLine();
Console.Write("Количество элементов не должно превышать размерность массива.");
}
for (int i = 0; i < count; i++)
{
sum += mas[i];
}
Console.WriteLine();
Console.Write("Сумма первых " + count + " элементов массива = " + sum);
Console.ReadKey();
}
Задание 5:
static void Main(string[] args)
{
int N = 10, x, y, sum = 0;
int[] mas = new int[N];
Random rnd = new Random();
for (int i = 0; i < N; i++)
{
mas[i] = rnd.Next(0, 10);
}
Console.WriteLine("Массив: ");
for (int i = 0; i < N; i++)
{
Console.Write(String.Format("{0,4}", mas[i]));
}
Console.WriteLine();
Console.WriteLine();
Console.Write("Введите x: ");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("Введите y: ");
y = Convert.ToInt32(Console.ReadLine());
if (x > N)
{
x = N / 2;
}
if (y > N)
{
y = N;
}
if (y < x)
{
x = N / 2;
y = N;
Console.WriteLine();
Console.Write("x не должен быть больше y.");
}
for (int i = x - 1; i < y; i++)
{
sum += mas[i];
}
Console.WriteLine();
Console.Write("Сумма элементов массива, с " + x + " элемента по " + y + " равна " + sum);
Console.ReadKey();
}
Объяснение:
Задания 4 и 5 на языке C#.
program SumSeq;
{$APPTYPE CONSOLE}
uses
SysUtils;
//const m:Integer ;
var
i, j, m, s:Integer;
n: array of Integer;
begin
s:=0;
Writeln(m);
Writeln('vvedite m');
Readln(m);
SetLength(n,m);
for i:=0 to m do
begin
j:=Random(10);
if j<5
then n[i]:=-1*j
else n[i]:=j;
write(n[i], ' ')
end;
Writeln;
Writeln('To continue push >>ENTER');
Readln;
for i:=0 to m do
begin
if n[i]<0
then s:=s+sqr(n[i])
end;
writeln('s=',s);
readln
{ TODO -oUser -cConsole Main : Insert code here }
end.
1250 -ответ 3)
Число задано в степенной форме, где Е=10 -основание десятичной системы счисления, т.е. 1.25*10^3=1250 -в естественной форме