0

I've been reading similar questions here for hours and I just can't seem to get it. I'm fairly experienced with java, but this is my first program in C. The professor has done absolutely nothing to teach us the relevant skills for this program.

Program Purpose: To read in text lines from a text file one pair at a time and determine whether the first is equal to, greater than, or less than the second based on the ASCII collating sequence. The program will also indicate the character position where the first deviation occurs. The program will process all pairs of lines and then print the total number of pairs processed, the number of equal pairs, the number of first line greater than the second, and the number of pairs with first lines less than second lines.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void readFile(char **strs){


    //read strings from file and place them in strs

}

void comparePairs(char *strs, int *nums){
    //compares pairs of strings according to ASCII collating sequence, 
    //printing strings as it goes. Also prints the position of the first deviation
    //between a pair, if there is any. 
    //returns numPairs, numEqual, numGreater, and numLesser
    int numPairs = 0;
    int numEqual = 0, numGreater = 0, numLesser = 0; 

    nums[0] = numPairs;
    nums[1] = numEqual;
    nums[2] = numGreater;
    nums[3] = numLesser;
}

void printResults(int numPairs, int numEqual, int numGreater, int numLesser){
    printf("%d pairs were processed.\n", numPairs);
    printf("%d equal pair(s).\n", numEqual);
    print("%d greater pair(s).\n", numGreater);
    print("%d less than pairs(s).\n", numLesser);
}

int main(){
    char strs[100][1000];//strings from file (max 100 strings, each of max size 1000)
    char (*ptrStrs)[1000];
    ptrStrs = strs;
    int nums[4] = {0,0,0,0};//contains numPairs, numEqual, numGreater, and numLesser

    int numPairs = 0;//the total number of pairs processed
    int *ptrNumPairs = &numPairs;

    //the number of pairs where the first line is equal to, greater than, or lesser
    //than the first line respectively
    int numEqual = 0, numGreater = 0, numLesser = 0; 
    int *ptrNumEqual = &numEqual, *ptrNumGreater = &numGreater, *ptrNumLesser = &numLesser; 

    readFile(**strs);
    comparePairs(strs, nums);
    printResults(nums[0], nums[1], nums[2], nums[3]);

    return 0;
}

I'm getting many errors, but the first is passing argument 1 of 'readFile' makes pointer from integer without a cast. Any help at all with making some headway with this would be greatly appreciated.

PsylentKnight
  • 155
  • 14
  • 2
    `char[100][1000]` is *not* the same as `char**`, and anyone telling you they are doesn't know what they're talking about. – WhozCraig Feb 03 '15 at 23:41
  • 1
    Related: there is also nothing in the requirements that suggest a lines-array usage is mandatory. This should be doable with a couple of line buffers and some counters. You may also want to ask the assigner whether the pairs are to be strict or succeeding (i.e. 1-2, 3-4, 5-6, etc... vs 1-2, 2-3, 3-4, etc...). That detail isn't crystal clear in your assignment. – WhozCraig Feb 03 '15 at 23:50
  • Its 1-2, 3-4, etc. Could you pointer me towards what sorts of things I need to do it with line buffers? – PsylentKnight Feb 03 '15 at 23:56
  • 1
    Just have two of them. Read two lines at a time. Compare the lines (`strcmp` will make short work of that unless you're forbidden from using it, in which case write your own). Always increment `numPairs`. Determine whether `numGreater` or `numLesser` should be incremented based on the non-zero result of `strcmp`. *Neither* will be incremented if `strcmp` returned zero. you need't track `numEqual`, as it is the same as `numPairs - (numLesser + numGreater)`. Continue that for each pair of lines read from the file. Thats it. Literally. – WhozCraig Feb 04 '15 at 00:02
  • 1
    Something [like this](http://pastebin.com/nS0LRi3h) (note, *not* error checked). I leave finding the position of difference to you. Best of luck. – WhozCraig Feb 04 '15 at 00:07
  • Thanks so much friend. I wasn't expecting anyone to basically write the whole thing but thanks anyway. One quick question, I understand char *argv[] is the file name, but what is int argc? – PsylentKnight Feb 04 '15 at 01:13
  • 1
    the number of arguments provided on the command line, +1 for the process itself. I should always be 1 or greater. Its in your C text and well-described online. – WhozCraig Feb 04 '15 at 01:39

0 Answers0