Answer the question
In order to leave comments, you need to log in
How to colorize values in C++ console?
I wrote the code, created a dynamic array, according to the task, you need to specify the maximum difference between two adjacent elements of the array and highlight these elements in red when outputting.
I colored only the maximum difference, for example, if we have such numbers: 1 3 2 6 - the maximum difference will be 4. How can I color two numbers where this difference is, in this case 2 and 6 need to be colored.
Here is the code:
#include <iostream>
#include <ctime>
#include <Windows.h>
using namespace std;
void massiv(int* x, int n)//функция заполнения и изменения
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for (int i = 0; i < n; i++)//заполнение массива
{
x[i] = rand() % 10;
cout << x[i] << " ";
}
int id = 0; // id - первый элемент пары, дающей максимальную разность
for (int i = 1; i < n - 1; i++)
if (abs(x[i] - x[i + 1]) > abs(x[id] - x[id + 1]))
{
id = i;
}
SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 4)); // покраска максимальной разности
cout << endl;
cout << abs(x[id + 1] - x[id]);
}
int main()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
srand(time(NULL));
int n;
while (!(cin >> n)) //проверка на число
{
cin.clear();
while (cin.get() != '\n');
cout << "NEVERNII VVOD" << endl;
}
if (n <= 0 || n == 1)
{
cout << "Vvedite zanogo";
cin >> n;
}
int* x = new int[n]; //динамическая переменная выделение памяти
massiv(x, n);//вызов функции
cout << "\n";
SetConsoleTextAttribute(hConsole, (WORD)((0 << 8) | 15));
cout << endl;
system("pause");
return 0;
};
Answer the question
In order to leave comments, you need to log in
If a script is terminated due to an error, then the scripts following it are executed in order of priority. And it does not affect the work of the previous scripts.
Moreover, if the script managed to do something, then this is also considered. For example, the script managed to declare a function and hang it as an event handler, and then an error occurred - the handler will remain and will work.
Further, if there is an error in this event handler, then it will occur every time the event occurs. And if the handler manages to do something before the error, create a variable, change the page, then all this is considered. So it seems that "everything" works, although it does, it's just that part of the non-critical functionality is cut off.
Specifically, in your case, see lines 141 in main.js - an error occurs there. If this is, for example, an attempt to show a banner, then there will be no banner. But everything else will work normally.
Write a function to print an array so you don't duplicate code.
Add a parameter to it (let's call it pos with a default value of -1) that will indicate the starting position of the elements to highlight in red;
Further in the function, you output the array up to this position, you output your a[pos] a[pos + 1] characters, then the remaining ones.
I would do things differently, but
void print(int* a, int size, int pos = -1)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int i = 0;
SetConsoleTextAttribute(hConsole, (WORD)((0 << 8) | 15));
for(; i < pos; ++i) cout << a[i] << " ";
if(pos != -1)
{
SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 4));
cout << a[i] << " " << a[i + 1] << " ";
SetConsoleTextAttribute(hConsole, (WORD)((0 << 8) | 15));
i += 2;
}
for(; i < size; ++i) cout << a[i] << " ";
cout << endl;
}
void massiv(int* x, int n)//функция заполнения и изменения
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
for(int i = 0; i < n; i++)//заполнение массива
{
x[i] = rand() % 10;
}
print(x, n);
int id = 0; // id - первый элемент пары, дающей максимальную разность
for(int i = 0; i < n - 1; i++)
{
if(abs(x[i] - x[i + 1]) > abs(x[id] - x[id + 1]))
{
id = i;
}
}
SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 4));
cout << abs(x[id + 1] - x[id]) << "\n";
print(x, n, id);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question