*Example* | Помогите по C++ (Массивы)

Ответить
Аватара пользователя
mrcnn

*Example* | Помогите по C++ (Массивы)

Сообщение mrcnn »

В массиве х(х1, х2...xn) найти максимальный элемент и поделить его на все остальные элементы?

Алглоритм простой, я не знаю как написат программный код на С++
Аватара пользователя
mrcnn

Re: *Example* | Помогите по C++ (Массивы)

Сообщение mrcnn »

Код: Выделить всё

#define N 12
#include <stdio.h>
void precise_division(int first,int second,int precision);
void main(void){
	int x[N]={1,2,3,34,4,5,6,7,8,9,10,11};
                //double b[N];
	int i=0;
	int result=x[i];
	for(i++; i<N;i++)
	if(x[i]>result)
		result=x[i];printf("Maximum: %d\n",result);
	if(result !=0){
		for (i=0; i<N;i++)
			//b[i]=(double)x[i]/(double)result;
			precise_division(x[i],result,20);
	}
}
void precise_division(int first,int second,int k){
	int c=0;
	int ost;
	int tmp;
	int cnt=0;
	int t=0;
	tmp=first;
	if(first>=second)
		while (first>=second)
		{
			first-=second;
			c++;
		}
	else if (first<second)
		c=0;
	if(first==0)
		printf ("first/second = %d\n",c);
	else
		{
		first=tmp;
		printf ("first/second = %d,",c);
		ost=first%second;
		while(cnt<k)
			{
			if( ost<second)
				{			
				if (t>=1)
					{
					printf ("0");
					cnt++;
					}
				t++;
				ost*=10;
				}
			else
				{	
				printf("%d",ost/second);
				ost%=second;
				cnt++;
				t=0;
				}
			}
		}
	printf ("\n");
}
Аватара пользователя
pva

Re: *Example* | Помогите по C++ (Массивы)

Сообщение pva »

Я бы посоветовал воспользоваться алгоритмами (писанины меньше и понятней)



Код:

Код: Выделить всё

#include <stdexcept>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
// must be declared 
class doSmth_divMax {
    double max;
public:
    doSmth_divMax(const double& d) : max(d)
   {
   }
    void operator()(double& d) const
   {
         d = max/d;
   }
};
void doSmth(vector<double>& vd)
{
    vector<double>::iterator imax = max_element(vd.begin(), vd.end());
    if (vd.end()!=imax) {
        for_each(vd.begin(), vd.end(), doSmth_divMax(*imax));
        return;
    }
    throw logic_error("there is no maximum here");
}

мне кажется, что в фразе
Цитата:



и поделить его на все остальные



кроится какая-то ошибка. Если так, то программа будет ещё проще.
Ответить

Вернуться в «Программирование и базы данных»