This is my aprox 2nd or 3rd time I have ever dealt with 2d arrays.
I am taking an unknown amount of data, putting it into a single array. Then spiting it into a 2d array.
Just knowing their probably is a different more efficient way to do this. Their has to be a better way to do this?
So that would be a challenge wouldn't it?
The thing I did get really stuck on is getting "strlen" off the second element of a 2d array. that is why I coded it like I did. Because I could not figure that one out.
what is a more efficient to take an unknown amount of data and deal with it accordingly to the specs.
specs
Code:
working with x amount of lines, or, unknown amount of data.
Open file then add it into a single array, then sort it
into a 2d array using only the first line and third line.
Then print output to screen. Using the lest amount
of lines of code needed to do so.
No using the compilers abilities to read everything on one line.
Must be formatted for readability.
I'd just like to know a more efficient way to write this.
my code.
Code:
#include <stdio.h>
#include <string.h>
#define MAXBUF 1024
int main (void)
{
FILE *fp;
char music[MAXBUF];
char check_music[2][MAXBUF];
fp = fopen("enter_sandman", "r");
char c = '\n';
int a = 0;
// fill array with word including end line
while (( c = fgetc(fp) )!= EOF )
music[a++] = c;
fclose(fp);
// a whole bunch of stuff to keep
// track of everything.
int b = 0, count = 0, check = 0, move_arr = 0, x = 0;
int string1 = 0, string2 = 0, z = 0;
//working with x amount of lines
// need 1st and 3rd
// ........ can the rest
//only need to look at 3 lines
while ( b != 3 )
{
// reset to go back into inner loop
check = 0;
while ( check != 1 )
{ // marks for 2nd line
if (count == 1)
{
//skips second line
// just runs it out to the end
while ( music[++x] != '\n');
// prevents it from returning inside here
count++;
}
else
{
//move_arr keep alignment with roll
// z keeps alignment with x
check_music[move_arr][z++] = music[x];
x++;
// switch off 1st and 3rd line
// to keep track of length of
// each line
switch (count)
{
case 0:
string1++;
break;
case 2:
string2++;
break;
default:
break;
}
// if end line
if ( music[x] == '\n')
{
// count is 0
// now it is one
// marked for 2nd line
count++;
// moves the 2d 1st element to
// get 3rd line
move_arr++;
// kicks it out of this
// inner loop
check = 1;
// resets lenght count
//to copy 2nd elements
// check_music[1][z = 0 -> length]
z = 0;
}
}
}
b++;
}
// self explanatory?
int g = 0;
for ( ; g < string1; g++)
printf("%c", check_music[0][g]);
printf("\n");
for ( g = 0; g < string2; g++)
printf("%c", check_music[1][g]);
printf("\n");
return 0;
}
output
Code:
userx@slackwhere:~/bin
$ ./last_3_words
Say your prayers, little one
To include everyone
data file, something everyone might identify with.
Code:
Say your prayers, little one
Don't forget, my son
To include everyone
Tuck you in, warm within
Keep you free from sin
Till the Sandman he comes
Sleep with one eye open
Gripping your pillow tight
Exit: light
Enter: night
Take my hand
We're off to never never land
Something's wrong, shut the light
Heavy thoughts tonight
And they aren't of Snow White
Dreams of war, dreams of liars
Dreams of dragon's fire
And of things that will bite
Sleep with one eye open
Gripping your pillow tight