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

http://pastebin.com/eC09W08p This project will focus on file processing. The ide

ID: 3537493 • Letter: H

Question

http://pastebin.com/eC09W08p



This project will focus on file processing. The idea behind file processing is simple: information that appear on screen and data that are given manually are volatile and prone to errors. Would you prefer having to key in 1,000 values when instead you can store them all in a file and read them from there? In this project, we will see a data structure that is very useful in a series of applications: internet, transportation, medical, social etc. A graph in mathematics is a series of vertices and edges, where each edge connects two vertices. A directed graph more specifically contains only edges that can start from a vertex (tail) and arrive to another vertex (head).

Explanation / Answer


#include <iostream>

#include <cstdlib>

#include <string>

#include <fstream>


using namespace std;

int main()

{

int tries=0;

char *filename;

ifstream input;

do{

if(tries>0)

{

cout<<"Error opening file. Please try again."<<endl;

if(tries%3 == 0) { cout<<"Please make sure that the file you are trying to open exists."<<endl; }

}

cout<<"Please give the filename:";

cin>>filename;

tries++;

input.open(filename);

}while(!input.is_open());


int choice;

int read=0;

int deg;

input>>deg;

ofstream output;

int values[deg][2];

while(true)

{


cout<<"COP2271 Data Manager MAIN MENU 1. To read graph from data file. 2. To compute the number of outgoing arcs from a certain node. 3. To compute the number of incoming arcs to a certain node. 0. To quit. Please choose:";

cin>>choice;


switch(choice)

{

case 1:

{

if(read==1) { cout<<"The file has already been read once"<<endl; break; }



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

{ input>>values[i][0]>>values[i][1];

}

cout<<"Graph successfully inputted! The graph has "<<deg<<" arcs."<<endl;

input.close();

read=1;

break;

}

case 2:

case 3:

{

if(read==0) { cout<<"The file has not been read yet."<<endl; break; }

int node;

cout<<"Please give the node you are looking for:";

cin>>node;

int nodeCount=0;

int x;

string x2;

if( choice ==2 ) { x=0; x2="leaving"; }

else if( choice ==3 ) { x=1; x2="going to"; }

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

{ if(values[i][x] == node) nodeCount++; }

cout<<"There are "<<nodeCount<<" arcs "<<x2<<" node "<<node<<endl;

output.open("output.txt",ios::out | ios::app);

output<<"There are "<<nodeCount<<" arcs "<<x2<<" node "<<node<<endl;

output.close();

cout<<"Report stored in file: output.txt."<<endl;


break;

}



case 0:

cout<<"Now quitting..."<<endl;

return 0;

break;


default:

cout<<"Wrong choice. Please choose again!"<<endl;

}

}

}