Jump to content

Help with C class please

joemomma22

okay so im just going to post the assignment itself, cause im a super noob with c. i am extremely fluent in java and i have a basic ability using MIPS and the class started doing C now.  i have literally no clue how to do most of the things in c... i could wwrite the entire program in java with no problem lol. Pointers *& kinda confuse me. i dont have syntax down either. 

 

heres the assignment:

In this lab you will write a C program to sort an array of strings. The program will:

  • Create an array of 50 strings; each string can hold up to 30 characters.
  • Write a function to read strings from the file "strings.dat" into your array. Read until EOF and keep track of how many strings you read, as it may be less than 50. Then pass this variable to your print and sort functions so they know how many strings to print and sort. Use fopen to open the file and fgets to read each string.
  • Write a function to sort the strings. Only sort the array entries that contain strings read from the file. See below for pseudocode for selection sort.
  • Write a function to swap two strings, which will be called by your sort function. Pass the array and the subscripts of the two strings that need to be swapped.
  • Write a function to print out the array entries that were read into the array. Use printf.
  • Your main function should call the functions to read into the array, print the array, sort the array, and print the array again.
Selection Sort

for (last = numstr-1; last >= 1; --last) {// select largest item in arr[0..last]loc = indexOfLargest(arr, last+1);// swap largest item arr[loc] with arr[last]swap(arr,loc,last);}int indexOfLargest(arr, numElements){indLarge = 0; // index of largest item found so farfor (curr = 1; curr < numElements ; ++curr) {if (arr[curr] > arr[indLarge])indLarge = curr;}return indLarge;}
Hints
  • Look up fopen. You will use "r" for the mode parameter; this opens the file for reading. The FILE pointer returned by fopen is used as a parm for fgets so that it reads from your file.
  • Look up fgets. Remember that fgets stores the newline in the string if it sees the newline before it reads n-1 characters (n is one of its parms). After the newline it adds a null byte.
  • fgets returns NULL when it reaches end of file.
  • After reading each string, remove the newline if it is there. If there is a newline, it will be in the next-to-last position of the string (right before the null byte). Use strlen to find the last position. If there is a newline, replace it with a null byte.
  • In C the constant for null is NULL. This constant is defined in stdio.h, as well as other header files. It must be written in all caps.
if anyone can help me with this i would really appreciate it.

 

also; what do you guys use to compile c... the class uses dev c++ but whenever i run the console for c apps on my laptop in devc++, devc++ stops responding... sooooo....

 

thanks guys xD - Joe

Edited by alpenwasser
code tags
Link to comment
Share on other sites

Link to post
Share on other sites

Can you link us the "strings.dat" file.

 

Use: http://www.codeblocks.org/ if DEV C++ does not work for you.

its an arbitrary file. i created one that was as followed:

string one

string two

string three

...

string fifty

Link to comment
Share on other sites

Link to post
Share on other sites

In windows mingw is pretty straight forward. I use it with the C edition of eclipse. Google for a setup guide.

C in Linux is easy, install gcc, and just compile with gcc <file>.c -o <fileName>

For sorting, I would use a bubble sort. It's horribly inefficient but really easy to write.

Create a swap function that takes 2 inputs, that will swap the pointers (easiest) or swap the data between two strings. If you decide to swap the data don't forget to call free after malloc. Both ways will require a temp variable.

Iterate over your array of strings and strcmp (string compare) i with i+1. If i is > i+1 then call your swap function to swap i with i+1, then increment i by 1. Repeat this process till you read the end of the array, if you had to call your swap method even one time, then your array is not sorted. Repeat the process above until no swaps have been made.

After that your array will be sorted.

EDIT: your teacher gave you pretty much everything you need to use selection sort. Just keep track of how many lines you read from the file and use your swap function to swap the largest with the last element. The variable last is decremented for you.

Your teachers swap method takes 3 inputs, the array, and 2 indexes. Same methodology applies though, swap the pointers or the dat.

http://www.geeksforgeeks.org/swap-strings-in-c/

http://www.phanderson.com/files/file_read.html

Link to comment
Share on other sites

Link to post
Share on other sites

In windows mingw is pretty straight forward. I use it with the C edition of eclipse. Google for a setup guide.

C in Linux is easy, install gcc, and just compile with gcc <file>.c -o <fileName>

For sorting, I would use a bubble sort. It's horribly inefficient but really easy to write.

Create a swap function that takes 2 inputs, that will swap the pointers (easiest) or swap the data between two strings. If you decide to swap the data don't forget to call free after malloc. Both ways will require a temp variable.

Iterate over your array of strings and strcmp (string compare) i with i+1. If i is > i+1 then call your swap function to swap i with i+1, then increment i by 1. Repeat this process till you read the end of the array, if you had to call your swap method even one time, then your array is not sorted. Repeat the process above until no swaps have been made.

After that your array will be sorted.

EDIT: your teacher gave you pretty much everything you need to use selection sort. Just keep track of how many lines you read from the file and use your swap function to swap the largest with the last element. The variable last is decremented for you.

Your teachers swap method takes 3 inputs, the array, and 2 indexes. Same methodology applies though, swap the pointers or the dat.

http://www.geeksforgeeks.org/swap-strings-in-c/

http://www.phanderson.com/files/file_read.html

im on windows 8.

cant use bubble sort, believe me i would, selection is mandatory but like you said its all there. im just having trouble grasping the concept of how to fill the array from the file using fscanf im assuming... and also the fget as well? not really sure how to use either of them properly

Link to comment
Share on other sites

Link to post
Share on other sites

im on windows 8.

cant use bubble sort, believe me i would, selection is mandatory but like you said its all there. im just having trouble grasping the concept of how to fill the array from the file using fscanf im assuming... and also the fget as well? not really sure how to use either of them properly

Look at the 2nd link at the bottom of my post above.

I have windows 8.1 as well, no issues for me using mingw and eclipse for C. The new versions of eclipse auto-detect any installed compilers. You just need to make sure you choose mingw as your compiler when creating a new project

Link to comment
Share on other sites

Link to post
Share on other sites

Look at the 2nd link at the bottom of my post above.

I have windows 8.1 as well, no issues for me using mingw and eclipse for C. The new versions of eclipse auto-detect any installed compilers. You just need to make sure you choose mingw as your compiler when creating a new project

ty im in the process of installing minGW now

Link to comment
Share on other sites

Link to post
Share on other sites

You need to be careful when using other compilers from the rest of the class.

Some compilers are smarter than other (gcc) so something that compiles fine with gcc might not compile with mingw. Or vice versa.

Some teachers want you to use a certain compiler, and others don't care. Send him an email and check if it's okay.

Otherwise I'd rough it with the udev compiler you mentioned above

Link to comment
Share on other sites

Link to post
Share on other sites

You need to be careful when using other compilers from the rest of the class.

Some compilers are smarter than other (gcc) so something that compiles fine with gcc might not compile with mingw. Or vice versa.

Some teachers want you to use a certain compiler, and others don't care. Send him an email and check if it's okay.

Otherwise I'd rough it with the udev compiler you mentioned above

yeah it was just hard to try using dev c++ because i cant freaking test anything... oh well more on that later i guess. i gotta get the dang coding part done

Link to comment
Share on other sites

Link to post
Share on other sites

i guess i messed up somewhere... cant debug nor compile 8s0O3.png

Link to comment
Share on other sites

Link to post
Share on other sites

i guess i messed up somewhere... cant debug nor compile

you have to find the compiler settings in eclipse, and set mingw gcc (that you previously installed)

Link to comment
Share on other sites

Link to post
Share on other sites

you have to find the compiler settings in eclipse, and set mingw gcc (that you previously installed)

Alright will do.

Can anyone help me with the code?

Link to comment
Share on other sites

Link to post
Share on other sites

Alright will do.

Can anyone help me with the code?

get the compiler to work as soon as possible, you just need to try out your code and experiment stuff
Link to comment
Share on other sites

Link to post
Share on other sites

get the compiler to work as soon as possible, you just need to try out your code and experiment stuff

i found the compiler setting, changed it to mingw and i still get the same binary not found error when i try to compile

Link to comment
Share on other sites

Link to post
Share on other sites

i tried to use devc++ on a different machine now it just wont compile. this is retarded x.x 8szDx.png

Link to comment
Share on other sites

Link to post
Share on other sites

Use Code::Blocks. It comes with MinGW and is already configured. Dev-C++ sucks, really.

 

http://www.codeblocks.org/

 

That said, it's probably choking on the fact you put a space in your file name.

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites

Use Code::Blocks. It comes with MinGW and is already configured. Dev-C++ sucks, really.

 

http://www.codeblocks.org/

 

That said, it's probably choking on the fact you put a space in your file name.

so does dcpp i tried codeblocks first. something wasnt working. ill try again

Link to comment
Share on other sites

Link to post
Share on other sites

okay i reinstalled code blocks. which compiler should i use for the c program then in the drop down. gnu? cygwin?

8sCpK.png

Link to comment
Share on other sites

Link to post
Share on other sites

no matter what compiler i use i am consistently getting this build error: 8sErG.png

Link to comment
Share on other sites

Link to post
Share on other sites

dont mean to spam my own post, but i got it. i realized i didnt install the code::blocks mingw version. well codeblocks works now. now how the heck do i get the program to read in the required strings from a .dat file?

Link to comment
Share on other sites

Link to post
Share on other sites

did you install mingw? did you install it in a custom path? if you leave it on c:/mingw, codeblocks should detect it right away

 

edit: did you run mingw to download the gcc compiler using the mingw packages installer?

 

edit again: nevermind, i didn't read your last post

Link to comment
Share on other sites

Link to post
Share on other sites

dont mean to spam my own post, but i got it. i realized i didnt install the code::blocks mingw version. well codeblocks works now. now how the heck do i get the program to read in the required strings from a .dat file?

int main(){    FILE *read;    read=fopen("something.dat","r");    if(read!=NULL)    {        char *s=malloc(10*sizeof(char));        fscanf(read,"%s",s);        printf("%s",s);    }    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

did you install mingw? did you install it in a custom path? if you leave it on c:/mingw, codeblocks should detect it right away

 

edit: did you run mingw to download the gcc compiler using the mingw packages installer?

 

edit again: nevermind, i didn't read your last post

^.^ lol okay well so far i have a basic outline of just reading in strings from the file into an array. not entirely sure on how to error check for the 30 character per string limit, but ill get to that later i guess.

Link to comment
Share on other sites

Link to post
Share on other sites

int main(){    FILE *read;    read=fopen("something.dat","r");    if(read!=NULL)    {        char *s=malloc(10*sizeof(char));        fscanf(read,"%s",s);        printf("%s",s);    }    return 0;}

what does the s pointer = malloc do/mean?

 

what i have so far: 

#include <stdio.h>#include <conio.h>int main(){    FILE *pFile;    char strings[51];    int i;    //Open File    pFile = fopen("Strings.dat", "r");    if (pFile != NULL)    {        while(!feof(pFile))        {            fgets(strings, 51, pFile);            printf("%s", strings);        }        fclose(pFile);    }    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×