Форум языка CPP

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.


Вы здесь » Форум языка CPP » Код домашнего задания » Работа с массивом (выбор и замена числа)


Работа с массивом (выбор и замена числа)

Сообщений 1 страница 2 из 2

1

Код:
srand((unsigned int)time(NULL));
	
	const int iSIZE = 20;
	
	int iArray[iSIZE];
	int iNumChoice, iNumReplace, iReplaceCounter = 0;
	

	/*
	*Array initialization
	*/
	for(int i = 0;i < iSIZE;++i)
	{
    iArray[i] = (rand() % 10) + 1;
	}


	/*
	*Displays the contents of the array
	*/	
	for(int i = 0;i < iSIZE;++i)
	{
    std::cout<<std::setw(3)<<iArray[i];
	}
	std::cout<<std::endl;
	std::cout<<std::endl;

	std::cout<<"Select the number of replacement"<<std::endl;
	std::cin>>iNumChoice;
	std::cout<<std::endl;
	std::cout<<"Number on which to change"<<std::endl;
	std::cin>>iNumReplace;
	std::cout<<std::endl;
	
	/*
	*Replacement of the array
	*/
	for(int i = 0;i < iSIZE;++i)
	{
    if(iArray[i] == iNumChoice)
    {
    	iArray[i] = iNumReplace;
    	++iReplaceCounter;
    }
	}

	/*
	*Displays the contents of the array
	*/	
	for(int i = 0;i < iSIZE;++i)
	{
    std::cout<<std::setw(3)<<iArray[i];
	}
	std::cout<<std::endl;
	std::cout<<std::endl;
	std::cout<<"Completed changes "<<iReplaceCounter<<std::endl;
	
	_getch();

0

2


Вы здесь » Форум языка CPP » Код домашнего задания » Работа с массивом (выбор и замена числа)