Please help me Write a C program that computes a students g
Solution
Solution for Question 1:
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
ifstream file(\"filename.txt\"); // opening the file with the default filename
string s;
int score,total=0;
float percentage;
if(file.is_open())
{
while(getline(file,s)) // getting the first and reading into String s
{
istringstream iss(s); // reading each element in the string as integer
}
file.close();
while(iss >> score)
{
total+=score; // Adding the values read from the file
}
score=total-score; // getting score out of the numbers read from file Ex : 800+1000 = 1800 and score will be //1800-1000 = 800
percentage = (float)(score/total); // converting the division into float
percentage*=100;
setprecision(5); //setting decimal precision to 5 points
cout << \"Score with decimals \" << percentage << endl;
}
else
cout << \"Unable to open the file\" << endl;
return 0;
}
Solution for Question 2:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name,firstname,midname,lastname;
cout << \"Enter the name : \" << \"\ \" <<endl;
getline(cin,name); // taking input as the string
int n=name.length(); // taking full string length of the input
lastname=getlastName(name); //calling method to get the last name
int n1=lastname.length(); // Storing the length of lastname in n1.
firstname=getfirstName(name);
int n2 = firstname.length(); // getting length of firstname
n2=n1+n2;
midname=getMidName(name); // storing Mid name in midname variable
cout <<firstname <<\" \"<< midname << \" \" << lastname<< endl; // :Printing output as expected
return 0;
}
string getlastName(string name)
{
string str=null;
for(int i=0;i<n;i++)
{
if(name[i]!=\',\')
{
str[i]=name[i];
}
else
break;
return str; // returning the last name
}
}
string getfirstName(string name)
{
string str; int k;
for(int i=0;i<n;i++)
{
if(i<=n1)
continue;
else if(name[i]!=\' \')
{
str[i]=name[i];
}
else if(name[i]==\' \')
break;
return str; // return firstname
}
}
string getMidName(string name)
{
string str;
for(int i=0;i<n;i++)
{
if(i<=n2)
continue;
else
{
str[i]=name[i]; // storing the string of midname in str.
}
return str;
}
Solution for Question 3:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
cout << \"Enter the input temperatures : \" << endl;
int n1=12; //taking 12 as series of temp
float n[12]; //storing them in an array
float j,Avg=0,diff;
ofstream file; // file initialization
file.open(\"tempdata.dat\"); // opening file to write the output
for(int i=0;i<n1;i++)
{
cin>>j;
n[i]=j; //Storing elements
Avg+=j;
if(i>0)
{
diff=n[i]-n[i-1];
file << n[i] << \" \" << diff << \"\ \";
}
else
file << n[i] << \"\ \"; // Writing only temperature value for the first input
}
Avg = Avg/n1;
file << \"Average of the values is \" << Avg << endl; // Writing Average Value to the file at the end
file.close();
return 0;
}




