C Question

For help and advice about coding

Moderator: Tech Moderators

User avatar
Steve
Posts: 5392
Joined: Sun Mar 18, 2007 5:49 pm
Tag: SSSSSSSSssssssssss
Contact:

C Question

Postby Steve » Tue Mar 31, 2009 2:15 pm

Sup,

I had a test on C programming today, and this was one of the questions. I got somewhere but wasn't really sure how to get it fully working.

/**********************************************************************

Write a program called 'invert' that inputs an ASCII picture (i.e.
lines of text) from a text file.and prints out the text file
up-side down. Please note that although the example below is quite
small your program should be able to deal with ASCII pictures that
could span very many lines of text.

FOR EXAMPLE: Suppose the text file foo.txt contained the following:
*
***
** **
** ***
** ****
Then the program could work like this:

$ invert foo.txt
** ****
** ***
** **
***
*

There is no one single answer to this problem and a variety of
different solutions may emerge. Please comment your code so that the
marker can gain an insight into the way you are thinking.

You may decide to prompt the user to input the filename or (for
better marks) use a command line argument as in the example.

***********************************************************************/


I came up with something similar to this

Code: Select all

#include <stdio.h>
#include "simpleio.h"

main()
{

  char filename[256] = "prog1.c";
  char buffer[256];
  FILE * file;
 
  file = fopen(filename,"r");
  if (file == 0)
  {
      printf("file %s does not exist\n", filename);
      return -1;
  }
  while (feof(file) == 0)
  {
           fgets(buffer,256,file);
           printf("%s\n", buffer);
  }
  fclose(file);



I was thinking you could use a stack (FILO) but how could you integrate it? something with buffer?

Or am I going in the wrong direction?

There's not much I can do now but I'd like to know what a possible solution could be. Ta.

Dan
Posts: 4105
Joined: Sun Mar 18, 2007 11:00 pm

Re: C Question

Postby Dan » Tue Mar 31, 2009 2:54 pm

C is a bit of a bastard, so the actual implementation of this would be harder than it's description.

Probably best to read the whole file into a single char array, and then to dynamically allocate memory for another char array of the same size as the first. Then, scan through the initial array, and on reaching a line break, put the substring from the start to the line break into the end of the new array (copying into size-length to size). Then repeat; take the second substring from the end of the last line to the next line break, and copy it into size-lengthcopied-length to size-lengthcopied and so on.

Maybe not as tidy as using a FILO stack, but probably faster to code (in C, at least), and certainly faster to run.

User avatar
nomis
Site Admin
Posts: 1412
Joined: Sun Mar 18, 2007 4:50 pm
Tag: Hasn't realised that there are custom ranks yet
Contact:

Re: C Question

Postby nomis » Tue Mar 31, 2009 3:04 pm

That sounds like far more work than it needs to be :shock:
I'll stick with PHP.

Code: Select all

$file = file('file.txt');
$flippedFile = array_reverse($file);
print_r($flippedFile);

User avatar
siteguru
Posts: 2124
Joined: Mon Apr 23, 2007 7:07 pm
Tag: SIR CUNTALOT
Location: Out of view ... but I AM watching you! MWUHAHAHAHA!!
Contact:

Re: C Question

Postby siteguru » Tue Mar 31, 2009 4:02 pm

The thing with C though is coding the array_reverse() function. ;)


PS - I've never done any C so I've no idea how it would be done. :oops:

User avatar
Blak
Tech Moderator
Posts: 1759
Joined: Mon Mar 19, 2007 12:07 pm
Tag: Hasn't realised that there are custom ranks yet
Contact:

Re: C Question

Postby Blak » Tue Mar 31, 2009 10:02 pm

Been a few years since I did C (and even then it was only a module at uni).

A FILO stack would be the way forward i thinks. Here be an implmentation of one

One loop while (feof(file) == 0) to populate the stack.

Then another that pops items off the stack and prints them till it's the stack is empty (popping off the stack results in a null value).

User avatar
Steve
Posts: 5392
Joined: Sun Mar 18, 2007 5:49 pm
Tag: SSSSSSSSssssssssss
Contact:

Re: C Question

Postby Steve » Wed Apr 01, 2009 4:18 pm

Awesome. Thanks guys.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 4 guests

cron