write a program to read in any. egm and have y our program write the image out a
ID: 3686036 • Letter: W
Question
write a program to read in any. egm and have your program write the image out as a PGM (grayscale, P2)
Your program should be able to work with any of the .egm Digital Elevation Maps,
The basic program should prompt the user for an input filename and output filename (or generate the output filename). It should prompt for a sun location, given as azimuth and elevation angles in degrees. It should print out the header information for the user.
It should then read the elevation map, do all calculations needed, and write output image(s) with no additional input needed.
A well-written program will break the task into separate functions for each subtask (as I have done in my solution), and be cleanly indented and well commented, and (most importantly) demonstrate to the grader and me that you have mastered the material.
A well written program will make extensive use of functions to make the code readable and easily extended and maintained.
c++
Explanation / Answer
#include<iostream>
using namespace std;
//sort function
void selection_sort(int [], int [], int n);
void print(int [], int [], int n);
int main(){
int MAXADS = 100;
int adcatcode[MAXADS];
int adwords[MAXADS];
int numads = 0;
int adcat;
while(numads < 100){
cout<< "Please enter Advertisement Category Code (1 - 15) (less than 1 to stop): ";
cin>>adcat;
if(adcat <= 0)
break;
adcatcode[numads] = adcat;
cout<<"Please enter number of words for the advertisement: ";
cin>>adwords[numads];
numads++;
}
//calling selection sort
selection_sort(adcatcode, adwords, numads);
//printing
cout<<"Sorted info"<<endl;
print(adcatcode, adwords, numads);
cout<<endl;
int currSum = adwords[0];
int i;
cout<<"Total number of words"<<endl;
cout<<"ad code"<<" "<<"words"<<endl;
for(i=1 ;i<numads; i++){
if(adcatcode[i-1] == adcatcode[i]){
currSum = currSum + adwords[i];
}
else{
//printing number of words previous adcode
cout<<adcatcode[i-1]<<" "<<currSum<<endl;
currSum = adwords[i]; // assigning with curr adcode
}
}
//printing last adcode info
cout<<adcatcode[i-1]<<" "<<currSum<<endl;
return 0;
}
void selection_sort(int adcatcode[], int adwords[], int n){
for(int i=0; i<n; i++){
int min_index = i;
for(int j=i+1; j<n; j++){
if(adcatcode[min_index] > adcatcode[j])
min_index = j;
}
//swapping
int temp = adcatcode[i];
adcatcode[i] = adcatcode[min_index];
adcatcode[min_index] = temp;
temp = adwords[i];
adwords[i] = adwords[min_index];
adwords[min_index] = temp;
}
}
void print(int adcatcode[], int adwords[], int n){
cout<<"cat code"<<" "<<"words"<<endl;
for(int i=0; i<n; i++){
cout<<adcatcode[i] <<" "<<adwords[i]<<endl;
}
}
/*
sample run:
Please enter Advertisement Category Code (1 - 15) (less than 1 to stop): 1
Please enter number of words for the advertisement: 2
Please enter Advertisement Category Code (1 - 15) (less than 1 to stop): 1
Please enter number of words for the advertisement: 2
Please enter Advertisement Category Code (1 - 15) (less than 1 to stop): 1
Please enter number of words for the advertisement: 2
Please enter Advertisement Category Code (1 - 15) (less than 1 to stop): 2
Please enter number of words for the advertisement: 2
Please enter Advertisement Category Code (1 - 15) (less than 1 to stop): 2
Please enter number of words for the advertisement: 2
Please enter Advertisement Category Code (1 - 15) (less than 1 to stop): 0
Sorted info
cat code words
1 2
1 2
1 2
2 2
2 2
Total number of words
ad code words
1 6
2 4
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.