(C++) Need simply help with C++ Read File and Sorting program.. Will rate helpful solution thumbs up
Data file:
title,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 Toy Story (1995),3,4,2,1,3,2,2,1,5,3,4,4,2,2,5,2,3,3,2,3 Jumanji (1995),4,3,5,5,3,3,3,2,4,5,2,1,3,2,3,4,2,4,2,2 Grumpier Old Men (1995),3,2,1,1,4,2,4,5,2,2,2,1,3,4,5,4,3,2,5,3 Waiting to Exhale (1995),4,5,4,5,5,1,3,5,5,1,2,3,4,2,4,4,3,5,5,5 Father of the Bride Part II (1995),5,4,2,4,1,1,1,3,2,3,2,4,3,3,1,5,4,3,2,5 Heat (1995),2,3,5,2,3,5,3,3,3,5,1,3,4,1,3,4,1,1,1,5 Sabrina (1995),3,1,5,1,1,5,4,1,3,1,5,4,5,1,1,2,3,2,5,2 Tom and Huck (1995),5,3,3,4,1,4,3,3,5,1,5,5,1,2,2,2,2,2,5,3 Sudden Death (1995),3,4,5,2,4,1,3,5,5,5,1,4,4,2,3,1,5,5,4,5 GoldenEye (1995),4,4,5,4,3,4,3,1,1,2,4,2,1,2,5,2,3,5,2,4 American President The (1995),5,2,2,4,1,3,4,2,3,4,4,3,5,2,3,4,5,3,4,1 Dracula: Dead and Loving It (1995),5,3,4,1,1,5,5,1,2,2,5,2,4,2,2,4,4,1,4,2 Balto (1995),1,1,5,2,4,3,3,1,2,2,3,2,4,2,3,3,4,2,1,3 Nixon (1995),5,4,5,3,2,2,3,2,2,3,3,1,4,2,5,5,4,5,3,5 Cutthroat Island (1995),4,2,3,2,4,5,1,5,1,2,5,3,3,4,3,1,3,3,4,3 Casino (1995),3,1,5,2,4,2,5,4,1,5,1,3,4,4,1,2,5,5,1,3 Sense and Sensibility (1995),4,4,5,3,4,1,5,1,3,1,3,3,3,4,4,4,2,5,1,1 Four Rooms (1995),3,5,2,5,5,4,2,3,2,3,3,4,2,4,4,4,4,3,3,2 Ace Ventura: When Nature Calls (1995),1,1,2,1,1,4,3,4,3,1,1,3,1,4,4,4,2,2,4,3 Money Train (1995),4,2,1,3,3,5,3,4,4,5,4,1,4,5,4,1,5,4,4,5 Get Shorty (1995),4,4,3,3,4,2,3,3,3,3,1,5,1,2,1,3,5,3,2,5 Copycat (1995),5,3,5,3,3,5,3,1,5,1,3,5,1,1,3,3,4,3,2,3 Assassins (1995),2,1,3,4,2,5,2,1,4,5,4,1,5,3,5,1,5,2,2,4 Powder (1995),4,2,5,4,3,1,4,4,5,2,3,2,1,3,5,1,5,2,2,1 Leaving Las Vegas (1995),4,4,1,5,2,4,4,1,2,5,3,5,2,3,3,4,3,4,5,5 Othello (1995),3,1,5,1,1,5,4,3,1,3,1,3,1,3,3,3,2,4,5,5 Now and Then (1995),3,5,2,4,3,2,4,1,2,3,4,4,4,5,1,3,5,1,2,2 Persuasion (1995),1,1,1,4,5,3,3,3,4,1,2,3,1,3,4,2,1,3,3,5 City of Lost Children (1995),2,2,5,4,2,3,2,3,3,2,4,4,5,2,2,2,4,3,5,2 Shanghai Triad (Yao a yao yao dao waipo qiao) (1995),5,5,5,2,2,1,3,2,5,2,3,5,2,2,5,2,5,2,3,2 Dangerous Minds (1995),1,5,2,5,2,1,5,2,5,5,5,1,1,3,2,1,4,4,4,1 Twelve Monkeys (a.k.a. 12 Monkeys) (1995),1,1,4,3,1,5,1,5,4,4,1,1,4,3,5,4,5,3,3,1 Babe (1995),2,3,2,4,2,2,1,2,5,4,1,3,1,1,1,1,4,2,5,1 Dead Man Walking (1995),2,3,2,5,5,1,5,1,4,5,5,4,4,1,5,1,5,1,4,3 It Takes Two (1995),3,4,4,1,4,1,4,5,3,1,4,4,5,2,4,2,3,1,2,3 Clueless (1995),2,4,5,1,4,2,3,4,1,3,5,3,1,1,3,3,5,1,3,1 Cry the Beloved Country (1995),2,2,4,4,4,4,3,2,1,3,1,1,4,2,5,4,3,4,3,1 Richard III (1995),1,1,5,2,4,2,3,5,2,5,4,1,1,4,1,1,2,3,2,5 Dead Presidents (1995),1,2,5,5,2,3,4,1,4,3,5,5,5,5,2,5,1,2,5,3 Restoration (1995),1,3,5,5,4,2,5,2,4,5,2,5,1,3,4,3,3,4,5,3 Mortal Kombat (1995),4,2,3,4,5,1,2,3,5,4,1,3,5,4,5,2,4,3,5,4 To Die For (1995),3,3,1,2,2,4,4,4,4,3,2,2,1,3,2,2,5,2,5,5 How to Make an American Quilt (1995),2,5,5,3,3,1,3,5,1,1,3,1,5,2,1,4,2,5,4,3 Seven (a.k.a. Se7en) (1995),3,1,1,3,1,1,4,1,1,5,1,4,3,5,4,5,5,2,1,5 Pocahontas (1995),4,4,5,2,1,3,4,5,1,2,1,1,2,4,1,2,5,4,5,2 When Night Is Falling (1995),5,3,3,5,1,3,5,3,1,2,3,5,4,2,2,2,2,4,2,5 The Usual Suspects (1995),1,5,1,5,2,3,2,2,5,3,4,2,4,1,3,3,4,3,4,1 Mighty Aphrodite (1995),5,2,5,1,5,1,2,4,1,4,5,5,2,5,5,2,5,5,5,2 Lamerica (1994),5,1,3,1,4,3,5,3,1,5,2,3,5,4,3,3,1,1,4,3 The Big Green (1995),2,5,5,1,5,2,5,5,4,1,4,1,4,3,1,1,1,4,5,5 Georgia (1995),1,4,5,2,1,5,4,3,5,3,3,3,4,1,4,1,2,3,2,1 Home for the Holidays (1995),1,3,5,1,3,3,3,5,5,1,2,3,1,1,3,4,4,4,5,3 Postman The (Postino Il) (1994),2,2,1,3,1,2,2,1,4,1,3,1,2,1,5,3,3,2,2,2 Indian in the Cupboard The (1995),3,5,1,5,5,3,4,5,3,4,2,4,5,2,4,4,5,3,2,2 Eye for an Eye (1996),1,3,4,5,1,4,4,3,1,5,2,4,4,5,5,4,3,3,2,3 Mr. Holland's Opus (1995),2,4,5,1,4,5,3,1,2,2,4,3,4,1,2,2,1,3,2,1 Don't Be a Menace to South Central While Drinking Your Juice in the Hood (1996),5,4,5,2,4,2,3,3,5,1,2,2,2,3,1,1,1,5,2,4 Two if by Sea (1996),2,4,2,4,1,2,3,2,5,3,1,3,1,1,1,2,3,3,4,5 Bio-Dome (1996),1,4,2,4,4,2,5,1,5,1,1,4,3,2,5,1,1,4,3,5 Lawnmower Man 2: Beyond Cyberspace (1996),4,5,3,1,3,1,1,2,2,2,5,4,2,4,4,2,5,3,3,4 French Twist (Gazon maudit) (1995),1,1,5,5,2,5,5,4,3,1,2,3,4,5,5,1,2,3,2,4 Friday (1995),4,4,4,2,4,1,1,3,3,5,4,5,3,3,2,2,5,3,5,4 From Dusk Till Dawn (1996),5,4,4,5,5,1,5,2,4,1,5,4,5,3,3,2,1,5,2,1 Fair Game (1995),4,1,3,4,4,2,1,5,5,5,1,2,1,5,1,1,5,3,3,2 Kicking and Screaming (1995),2,2,3,3,5,1,4,1,2,2,5,2,5,4,2,1,1,2,2,1 Misrables (1995),1,5,1,3,5,4,3,1,1,1,1,1,4,5,5,1,4,1,3,2 Bed of Roses (1996),3,5,4,4,3,2,3,5,1,3,1,2,1,5,3,3,5,3,4,2 Big Bully (1996),5,5,2,2,3,5,5,1,1,4,1,1,4,2,1,2,3,1,2,4 Screamers (1995),5,4,5,4,1,2,4,3,4,1,4,3,1,4,3,4,2,5,2,4 Nico Icon (1995),5,4,3,4,5,1,3,2,3,5,2,5,2,1,2,1,4,2,2,1 Crossing Guard The (1995),4,2,4,3,5,2,2,5,4,1,3,5,5,5,4,4,4,1,3,4 Juror The (1996),3,4,5,4,3,5,5,1,2,4,3,4,2,5,3,4,3,5,5,3 White Balloon The (Badkonake sefid) (1995),5,2,2,3,3,4,1,3,2,5,5,1,2,2,4,4,5,2,4,5 Things to Do in Denver When You're Dead (1995),1,2,1,3,4,4,3,2,2,2,1,5,2,4,4,3,2,1,2,3 Antonia's Line (Antonia) (1995),5,5,3,5,4,2,2,5,3,3,3,3,4,5,4,2,3,5,2,4 Once Upon a Time... When We Were Colored (1995),5,3,3,2,4,3,5,3,1,4,4,4,5,1,3,3,5,4,1,1 Angels and Insects (1995),2,5,5,2,4,5,5,5,1,5,2,1,4,1,1,4,5,3,5,4 White Squall (1996),2,2,4,2,4,1,4,2,4,3,5,4,4,1,5,1,3,1,1,4 Dunston Checks In (1996),1,3,2,5,1,4,4,1,2,4,2,2,3,4,4,2,3,4,1,4 Black Sheep (1996),4,5,4,3,2,4,2,5,5,3,3,4,1,4,2,3,2,3,5,2 Nick of Time (1995),1,4,3,4,5,1,5,1,4,2,1,3,5,3,2,1,1,4,4,2 Mary Reilly (1996),2,3,4,5,2,2,3,5,3,5,2,2,2,3,4,3,2,5,4,5 Vampire in Brooklyn (1995),4,5,2,4,1,4,1,2,4,5,1,5,5,3,5,4,5,5,4,1 Beautiful Girls (1996),4,2,1,2,1,3,5,2,2,3,1,3,3,2,2,3,1,5,1,4 Broken Arrow (1996),5,2,1,3,4,1,1,5,5,3,5,2,4,1,4,1,1,3,5,5 In the Bleak Midwinter (1995),5,4,5,5,2,2,2,2,4,5,4,4,1,2,1,2,3,3,4,2 Hate (Haine La) (1995),2,4,4,3,2,2,1,1,5,5,2,2,5,1,3,2,4,1,1,5 Heidi Fleiss: Hollywood Madam (1995),5,4,3,5,3,3,1,5,4,4,5,5,5,1,3,1,3,2,1,1 City Hall (1996),4,5,4,2,5,1,2,5,4,4,3,4,3,1,5,1,1,1,3,2 Bottle Rocket (1996),4,3,4,4,3,1,3,2,4,5,2,2,3,3,4,1,3,4,5,5 Mr. Wrong (1996),3,4,2,4,2,4,1,2,3,4,3,5,1,5,5,3,4,2,3,1 Unforgettable (1996),2,2,5,3,2,4,4,4,4,2,3,1,1,4,2,3,4,1,3,2 Happy Gilmore (1996),2,1,5,5,3,2,2,2,1,3,1,2,1,5,1,5,2,1,3,1 Bridges of Madison County The (1995),1,5,2,3,2,3,2,2,5,5,3,5,4,2,1,2,3,3,2,2 Nobody Loves Me (Keiner liebt mich) (1994),1,2,5,5,4,3,5,5,1,5,3,2,4,4,3,1,2,3,5,3 Muppet Treasure Island (1996),5,2,1,2,3,3,5,5,4,1,1,4,2,5,4,3,3,1,3,4 Catwalk (1996),4,5,1,1,5,1,3,1,3,5,2,4,5,1,2,2,5,1,4,2 Braveheart (1995),4,3,1,5,5,2,5,1,3,5,2,4,1,5,5,1,2,3,4,1 Taxi Driver (1976),1,1,4,4,1,2,1,1,2,4,3,3,4,4,5,4,5,2,5,1 Rumble in the Bronx (Hont faan kui) (1995),3,4,4,2,1,2,2,4,4,4,3,2,3,3,4,4,5,4,2,5
code
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include <utility>
using namespace std;
int main()
{
string movieName[100],tempstr;
int ratings[100][20],id;;
double avg[100],sum,avgUserRating,temp;
ifstream infile;
infile.open("data.txt");
if(!infile)
{
std::cout<<"Unable to open the file!"<<endl;
return 0;
}
string line;
int i=0,j=0;
while(getline(infile,line))
{
string word;
stringstream stream(line);
i=0;
while(getline(stream, word, ','))
{
if(i==0)
movieName[j]=word;
if(i==1)
ratings[j][0]=stoi(word);
if(i==2)
ratings[j][1]=stoi(word);
if(i==3)
ratings[j][2]=stoi(word);
if(i==4)
ratings[j][3]=stoi(word);
if(i==5)
ratings[j][4]=stoi(word);
if(i==6)
ratings[j][5]=stoi(word);
if(i==7)
ratings[j][6]=stoi(word);
if(i==8)
ratings[j][7]=stoi(word);
if(i==9)
ratings[j][8]=stoi(word);
if(i==10)
ratings[j][9]=stoi(word);
if(i==11)
ratings[j][10]=stoi(word);
if(i==12)
ratings[j][11]=stoi(word);
if(i==13)
ratings[j][12]=stoi(word);
if(i==14)
ratings[j][13]=stoi(word);
if(i==15)
ratings[j][14]=stoi(word);
if(i==16)
ratings[j][15]=stoi(word);
if(i==17)
ratings[j][16]=stoi(word);
if(i==18)
ratings[j][17]=stoi(word);
if(i==19)
ratings[j][18]=stoi(word);
if(i==20)
ratings[j][19]=stoi(word);
i++;
}
j++;
}
std::cout<<"\n\n";
for(i=0;i<100;i++)
{
sum=0;
for(j=0;j<20;j++)
{
sum+=ratings[i][j];
}
avg[i]=sum/20;
}
cout<<"Enter the Valid user id: ";
cin>>id;
for(i=0;i<100;i++)
{
sum+=ratings[i][id-1];
}
avgUserRating=sum/100;
cout<<id<<"'s average rating is: "<<avgUserRating<<endl;
return 0;
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then plese appriciates with up vote. Thank You.
(C++) Need simply help with C++ Read File and Sorting program.. Will rate helpful solution thumbs...