Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sunspot data for the past 275 years or so has been collected in the file sunspot

ID: 3715514 • Letter: S

Question

Sunspot data for the past 275 years or so has been collected in the file sunspots.dat The data in the file is organized into three columns as follows: the first column is a year number (int), the second column is a month number (int), and the third column is the average daily number of sunspots seen in that year/month (float). The very first line of the file contains a single integer, which identifies the number of data points in each of the three following columns Write a C++ program which will load each column of data in this file into three separate arrays: an int array called year, another int array called month, and a float array called spots. Your program should then analyze the data in these arrays and print the following information to the screen: 1.) The largest number of sunspots seen, and the year/month when this occurred; 2.) The average number of sunspots seen over the time range in the data.

Explanation / Answer

// C++ program to read a file and analyze the data

#include <iostream>

#include <fstream>

using namespace std;

// function declaration

int largest_sunspot(float [],int size);

float average_sunspot(float[] ,int [],int[], int size);

int main() {

               int points;

               ifstream fin("sunspots.dat"); // open the file

               // if file exists

               if(fin.is_open())

               {

                              fin>>points; // read the number of data points

                              // create array for year, month and sunspots

                              int year[points];

                              int month[points];

                              float sunspots[points];

                              int i=0;

                              // read the data points

                              while(!fin.eof())

                              {

                                             fin>>year[i]>>month[i]>>sunspots[i];

                                             i++;

                              }

                              // display the largest sunspot month and year and average number of sunspots in the time range

                              int index = largest_sunspot(sunspots,points);

                              cout<<" The largest sunspots : "<<sunspots[index]<<" occurred in month : "<<month[index]<<" Year : "<<year[index];

                              cout<<" Average number of sunspots seen :"<<average_sunspot(sunspots,year,month,points)<<endl;

               }else

                              cout<<" Unable to open file";

               fin.close();

               return 0;

}

// function to return the index of largest sunspot

int largest_sunspot(float sunspot[],int size)

{

               int max =0;

               for(int i=1;i<size;i++)

                              if(sunspot[i] > sunspot[max])

                                             max =i;

               return max;

}

// function to return the average sunspots in the time range

float average_sunspot(float sunspot[] ,int year[],int month[], int size)

{

               int year_min = year[0],year_max = year[0];

               float total_sunspots = 0;

               for(int i=0;i<size;i++)

               {

                              if(year[i] > year_max)

                                             year_max = year[i];

                              if(year[i] < year_min)

                                             year_min = year[i];

                              total_sunspots += sunspot[i];

               }

               return(total_sunspots/(year_max - year_min));

}

//end of program

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote