-2

I need to make a program that takes in:

  1. The total number of characters to be compared
  2. The character to evaluate
  3. A comparisons between the character to evaluate and other characters

The output of the program should be "YES" if the selected character is the greatest, and otherwise "NO".

The input is obtained from 'input.txt', where:

  1. The first number, n, is the number of characters
  2. The second number, k, is the selected character
  3. Each subsequent line is a character comparison with the greater on the left and the smaller on the right
  4. A terminating line containing only a '0'

But how can I compare the values when there are more than 2 characters?

For example this 'input.txt' should result in "YES" because k is '1', and '1' is greater than the other 2 characters:

3 1
1 2
1 3
0

This example should result in "NO" because k is only greater than 1 character:

3 2
2 3
0

Here is my code, I had intended to use x and y to represent the first and second character on a comparison line:

#include <iostream>
#include <fstream> 
using namespace std;

void main(){
int n;
char k;
char x;
char y;

char max=1;
unsigned char end;

ifstream input;
input.open("input.txt");
input >> n;
input >> k;

while (end!=0){

    /*Here is the part I want an advance
    the program needs to compare many character ids (almost 50 of them),
    but I cannot imagine how to do that*/

    end = 0;
}

ofstream output;
output.open("output.txt");

if (k == max)
    output << "YES";
else
    output << "NO";
}
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
Hayk
  • 17
  • 3
  • 3
    First job: pick your language. – Bathsheba Dec 11 '15 at 11:42
  • I'm using C++. Sorry, I forgot to add – Hayk Dec 11 '15 at 11:44
  • 1
    Second job: show us sample code and point out which part of the code is the point where you can't advance. – tnull Dec 11 '15 at 11:49
  • 1
    If i understood your problem right, you want to open a file for reading and find the greatest character in it. What i do not undestand is on what condition the program should print no. To me, if the file is not empty of course, the greatest character should be found. It's not really clear form your question, why your program should return `no` for your second example. – Eugene Anisiutkin Dec 11 '15 at 11:55
  • You say, `n` is the number of characters but then you have an input of: "3 2\n2 3\n0" I don't think I'm understanding `n`'s roll here? – Jonathan Mee Dec 11 '15 at 12:08
  • 3 means that there are 3 characters, 2 means that selected character is the '2', but on 2nd lane it displays information only about characters '2' and '3', no information on '1' character, so program outputs "NO" – Hayk Dec 11 '15 at 12:13
  • @Hayk You're getting a lot of negative publicity because of the ambiguity of your question. I have more fully specified the question, but I made an assumption to do this: I assumed that `k` will always be on the left hand side of each comparison line. If that's not the case, you're looking at a directed graph problem and that's a *lot* harder. If my assumption is wrong please edit or reply so I can correct the edits. – Jonathan Mee Dec 11 '15 at 14:25
  • The K is the 2nd character. The rows show the comparison of id's except for the 1st row, that shows the amount of character ids and the selected character, for example in second row '1 2' means 1>2 – Hayk Dec 11 '15 at 15:19

1 Answers1

1

Assuming each of your lines (after the first one) is a comparison with the greater on the left. Your solution is basically to read in all the lines and see if k is ever the second character. Based on your second example if a comparison for each character isn't available, the default response is "NO", so you'd also need to count the number of lines. One way you could accomplish this is:

bool isKGreatest = true;
char first;
char second;

while(input >> first >> second) {
    n--;

    if(second == k){
        isKGreatest = false;
        break;
    }
}

if(n > 1) {
    isKGreatest = false;
}

Then all you'll need to do is output "YES" if isKGreatest is true and "NO" if isKGreatest is false. I've made an example to play with here: http://ideone.com/YMk5mk

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288