| General | Hosted Sites | Civ5 | CivRev | Civ4Col | Civ4 | Civ3 | Civ2 | Civ1 | Misc | Marketplace |
![]() |
|
|
Welcome to Civilization Fanatics' Center. You are currently viewing our site as a guest which gives you limited access to our site features. By joining our free community, you will be able to participate in the discussions, search the forum, send private messages, vote in polls, upload your own screenshots to the gallery, and access many other special features. Registration is fast, simple and absolutely free, so sign up today! If you have any problems with the registration process or your account login, please contact support. |
|
|||||||
![]() |
|
|
Thread Tools |
|
|
#1 |
|
We're gonna live forever
|
Help with C algorithm
Hi
![]() (main question in post 8) I'm trying to do a more complex version of the parallell bubble sort. I have some questions about it. I'll ask as i get to them. ![]() Here is the code: (ack, a pity spaces are ignored on the forum, the code looks kindof a mess like this ...) Spoiler:
Please excuse the noobness of my skills. The thing is when A[ ] has an odd number of elements in it, the algorithm doesn't use the last one. The way i figure, the n/2 is to blame, but i can't find a way to fix it. How could i do it in a "subtle" way ? There is the blunt way. If A has an odd number of elements then the last element will just search for it's place in A after the bubble sort takes place. But i don't think that's the way it should be made. Last edited by Heretic_Cata; Jan 09, 2007 at 12:55 PM. |
|
|
|
|
|
#2 | |
|
Evergreen
Join Date: Sep 2002
Posts: 7,476
|
To preserve spacing, use a [ code ] tag,
Code:
like
this
for (i=0; i<n; ++i) instead of: for (i=0; i<=n-1; ++i) (note that it's also more efficient) Secondly, try to add accolades to the for loops (even if you only have one instruction inside). Third, this statement: if(k%2==0) { can be removed. Simply modify the for loops like this: for(i=0; i < n/2; i+=2) and for(i=0; i < n/2 - 1; i+=2) This way, you don't need to test parity on each iteration, you just iterate directly through the odd or even indexed elements. Well, I can't figure out what kind of bubble sort this is. Maybe you're looking for a thing called bidirectional or "cocktail" sort Quote:
I hope this helps.
__________________
[center][color="DarkBlue"][size=1]All rational action is in the first place individual action. Only the individual thinks. Only the individual reasons. Only the individual acts. --Ludwig von Mises-- |
|
|
|
|
|
|
#3 | |
|
We're gonna live forever
|
^
Ok, i'll try those things a bit later ... But: Quote:
It would be really funny if the teacher would say "This is not Parallell bubble sort" I actually didn't know there was a parallel version of Bubble sort. I found it through google: lookie. |
|
|
|
|
|
|
#4 | ||||
|
We're gonna live forever
|
Quote:
![]() Quote:
Quote:
It seems to order all the numbers including the one that was ommited, but it moves "7" to the 5th place in list. ![]() Quote:
![]() Multumesc mami. (but the thread isn't over yet ... i have a feeling i'll stumble into some more questions a bit later ...) |
||||
|
|
|
|
|
#5 |
|
We're gonna live forever
|
EDIT: hey, i managed to do this
, nvmOk, problem no 2. My objective now would be to make my bubble sort get data from a file. Sounds easy enough ... in Java. But in C ... ![]() One step at a time. First of all, i don't know what fuction i have to use for such a thing. From what my teacher wrote i learned how to get characters, words from files and to count integers. With functions one uglier than the other. Which is not what i need. The closest function that i would imagine getting my numbers from files would be fscanf. Is it the right one ? I tried to use it over here: Code:
void main() {
clrscr();
FILE *fp = fopen("test.txt","r");
if (fp!=NULL){
int A;
fscanf(fp,"%d", A);
printf("Am citit nr %d. \n", A);
fclose(fp);
}
getch();}
Also, i have another question: How does a txt file that is the source of a program supposed to look ? I mean, in Java most are like so: Code:
31 354 677 45 47 87 42 32 Last edited by Heretic_Cata; Jan 07, 2007 at 12:27 PM. |
|
|
|
|
|
#6 |
|
Evergreen
Join Date: Sep 2002
Posts: 7,476
|
this line:
fscanf(fp,"%d", A); change it to: fscanf(fp,"%d", &A); fscanf (and the rest of the scanf family) require the last parameters to be addresses and not variable names. &A is the address of variable A. So why does it show you -16329? Note that A is defined, but not initialized. This means A will begin its life with a gibberish value (whatever is found in memory). Since fscanf does not change the value of A, A remains uninitialized. Also, fscanf will try to write data to memory cell "-16329" and it can end up corrupting your system if you don't use an OS with memory protection (if it's Windows ME or above, you're OK).
__________________
[center][color="DarkBlue"][size=1]All rational action is in the first place individual action. Only the individual thinks. Only the individual reasons. Only the individual acts. --Ludwig von Mises-- |
|
|
|
|
|
#7 | |
|
We're gonna live forever
|
Quote:
After one day of doing loads of crap, i saw how it looked. It took me almost 1 day to find that. And after i found it, it took 5 minutes to figure out how to make a program that reads some numbers from a file, puts them in A[ ] and parallel bubble sort them. ![]() The last part of the project will be with making parts of the program run with parallel processes. But i didn't try it yet. I'll probably have some questions on those too ... |
|
|
|
|
|
|
#8 |
|
We're gonna live forever
|
The wonderfull word of processes is a bit too much for me.
I can't figure out what the most basic of programs do. 1. First, i have a stupid questions. What exactly are argc and argv[] ? (in general; see examples below) 2. Code:
int main(int argc, char *argv[])
{
system("dir");
}
3. Code:
int main(int argc, char *argv[])
{
execlp("cmd.exe", NULL);
}
The exemple in C help has something like: Code:
execlp("spawn.exe", NULL);
but it doesn't mention anything about spawn.exe everywhere else. (did i mention i don't know what exactly a process is ?) I'm lost.
Last edited by Heretic_Cata; Jan 09, 2007 at 01:57 PM. |
|
|
|
|
|
#9 | |
|
Excentric
Join Date: Mar 2002
Location: Kentucky
Posts: 2,887
|
Quote:
*argv [] == a pointer to the array of the actual arguments. Example: Code:
/* test_args.c */
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("Number of Args: %d\n", argc);
for (i=0; i<argc; ++i)
{
printf("Arg %d: %s\n", i, argv[i]);
}
return 0;
}
$gcc test_args.c -o test_args
$./test_args arg1 arg2 arg3
Number of Args: 4
Arg 0: ./test_args
Arg 1: arg1
Arg 2: arg2
Arg 3: arg3
__________________
Time is what keeps everything from happening all at once. |
|
|
|
|
|
|
#10 | |
|
We're gonna live forever
|
What are these ?
So argv[0] will always be the path of the source file. Argv[1] is aparently "k". ![]() Quote:
|
|
|
|
|
|
|
#11 | ||
|
Excentric
Join Date: Mar 2002
Location: Kentucky
Posts: 2,887
|
Quote:
Code:
C:>test_args arg1 arg2 arg3 Quote:
Code:
FILE *pipe;
char buf[80];
pipe = popen("/bin/ls", "r");
if (!pipe)
{
perror("popen");
return 1;
}
while (fgets(buf, sizeof(buf), pipe) != NULL)
{
printf("%s", buf);
pclose(pipe);
}
return 0;
__________________
Time is what keeps everything from happening all at once. |
||
|
|
|
|
|
#12 | |
|
We're gonna live forever
|
Quote:
![]() I named the program "testarg". But for this to work doesn't there have to be an .exe ? (I'll try your program in a bit.) |
|
|
|
|
|
|
#13 |
|
Excentric
Join Date: Mar 2002
Location: Kentucky
Posts: 2,887
|
Yes there has to be an executable file to run the program, and if I remember right dos does use extensions such as .exe, .com, etc..., instead of determining type by actually looking at what is in the file.
__________________
Time is what keeps everything from happening all at once. |
|
|
|
|
|
#14 | |
|
We're gonna live forever
|
Code:
FILE *pipe;
char buf[80];
pipe = popen("/bin/ls", "r");
if (!pipe)
{
perror("popen");
return 1;
}
while (fgets(buf, sizeof(buf), pipe) != NULL)
{
printf("%s", buf);
pclose(pipe);
}
return 0;
![]() C Help doesn't seem to have heard of popen/pclose. Do you mean fopen/fclose ? (but those are for opening a data file ... they can't work)Quote:
"testarg.exe arg1 arg2 arg3" - that didn't work either. ![]() But i have heard that it is possible to create an exe of your source code. Someone did it for my pacman program. But i can't find anything in my Borlandc that can do that. I don't know how he did it, and he's not going to be around for a while so i don't know who to ask. |
|
|
|
|
|
|
#15 | |
|
Excentric
Join Date: Mar 2002
Location: Kentucky
Posts: 2,887
|
Quote:
__________________
Time is what keeps everything from happening all at once. |
|
|
|
|
|
|
#16 |
|
We're gonna live forever
|
You mean:
Alt+F9 = compile and Ctrl+F9 = run ? Of course i did that. But they don't create exe files. |
|
|
|
|
|
#17 | |
|
Excentric
Join Date: Mar 2002
Location: Kentucky
Posts: 2,887
|
Quote:
Code:
C:>bcc32 TestArgs.cpp
__________________
Time is what keeps everything from happening all at once. |
|
|
|
|
|
|
#18 | ||
|
We're gonna live forever
|
Quote:
I've heard that in newer versions, exe's are created automatically.Quote:
In the menus there is a "Build all" option. I've clicked it before, but i never looked what it was doing. It created an exe in a far away folder. ![]() I'll try all the stuff now.
|
||
|
|
|
|
|
#19 |
|
Excentric
Join Date: Mar 2002
Location: Kentucky
Posts: 2,887
|
"Build all"...that was it. There is a program called Rhide that has the look and feel of the old Borland IDE that has been ported to Linux but I don't have it installed. I find it easier to use the command line when you are writing small programs.
__________________
Time is what keeps everything from happening all at once. |
|
|
|
|
|
#20 |
|
We're gonna live forever
|
Yup, the first one worked perfectly. I understand it.
![]() Now on to the next question: Code:
FILE *pipe;
char buf[80];
pipe = popen("/bin/ls", "r");
if (!pipe)
{
perror("popen");
return 1;
}
while (fgets(buf, sizeof(buf), pipe) != NULL)
{
printf("%s", buf);
pclose(pipe);
}
return 0;
![]() C Help doesn't seem to have heard of popen/pclose. Do you mean fopen/fclose ? (but those are for opening a data file ... they can't work)(i also changed "ls" to "dir" in my code) |
|
|
|
![]() |
| Bookmarks |
|
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Global Warming algorithm | gilbie01 | Civ4 - General Discussions | 2 | Jan 30, 2006 08:03 PM |
| The algorithm according to which the AI decides... | onomastikon | Civ3 - Strategy & Tips | 11 | Aug 20, 2005 01:30 AM |
| Can you figure out the algorithm? | stratego | All Other Games | 6 | Jun 22, 2005 02:38 PM |
| Making an artillery bombard algorithm | player1 fanatic | Civ3 - General Discussions | 8 | Jan 17, 2004 12:54 PM |
| MATLAB help: Dijkstra's Algorithm | stratego | Computer Talk | 3 | Dec 06, 2003 06:45 AM |