Beispiel #1
0
void P6::Monochrome(const char* filename)
{
	if (isGrayMonochrome() == true)
	{
		cout << "The image is already monochrome \n";
		return;
	}
	else
	{
		char* newName = ChangeFileName(filename, "_monochrome.ppm");
		ofstream outFile(newName, ios::out);
		if (!outFile)
		{
			cerr << "Cannot write P6 file \n";
			return;
		}
		char magic_pbm[3] = "P6";
		outFile << magic_pbm << endl << getWidth() << " " << getHeight() << endl << getMaxNum() << endl;

		int r = 0, g = 0, b = 0;
		for (int i = 0; i < getHeight(); i++)
		{
			for (int j = 0; j < getWidth(); j++)
			{
				r = getPixels()[i][j].getRed();
				g = getPixels()[i][j].getGreen();
				b = getPixels()[i][j].getBlue();

				int gray = grayscale(r, g, b);
				int m = monochrome(gray);

				outFile.write((const char*)&m, sizeof(char));
				outFile.write((const char*)&m, sizeof(char));
				outFile.write((const char*)&m, sizeof(char));
			}
		}

		outFile.flush();
		outFile.close();
		delete[] newName;
	}
}
Beispiel #2
0
int* majorityElement(int a[],int len)
{
	if( a==NULL )
	    return NULL;
       
        printf("the arrary size is %d \n",len);
	
	int values[100] = {0}; //sotre the unique value
        int times[100]  ={0};  //times[i] store the number of times of values[i] appears.
        int valueTail = 0;    // indicate how many unique value.
        int i= 0;
        for( i=0;i< len;i++)
	{   
            int j=0;
	    for(j=0; j<valueTail;j++)
            {
		if(a[i]==values[j])
  		{
		   times[j]++;
		   break;
                }
            }
	    if(j==valueTail)
            {
            	values[valueTail]=a[i];
                times[valueTail++]++;
            }
	}
        for(i=0; i<valueTail;i++)
        {
           printf("the times arrary values is %d, times[%d]= %d \n",values[i],i,times[i]);
        }
       int max = getMaxNum(times,valueTail);
       printf("the majority num is %d \n ", values[max]);

	
}