The multi-process and output - c
I am doing a homework about multi-processing.
In this homework we are asked to use fork() function to fork different process from one parent process. Then we do same simulation on different child process. The problem is related to sports game. The program should be able to simulate the grouping function for different teams.
Now I am focusing on the first part that is test mode and I meet some problems.
I got the output like
Parent, pid 57361 : 2 children T Mode
Child 1 , pid 57362 : teams for pot 0 are : Liverpool Arsenal Barcelona ManCity Juventus Bayern Paris Zenit
Child 1 , pid 57362 : teams for pot 1 are : RealMadrid Atletico Chelsea Dortmund Napoli Shakhtar Tottenham Ajax
Child 1 , pid 57362 : teams for pot 2 are : Benfica Lyon Leverkusen Salzburg Olympiacos Brugge Valencia Internazionale
Child 1 , pid 57362 : teams for pot 3 are : Zagreb Lokomotiv Genk Galatasaray Leipzig SlaviaPraha Atalanta Lille
Child 1 , pid 57362 : Teams for group A are RealMadrid Brugge Galatasaray Paris
Child 1 , pid 57362 : pots for group A are 2 3 4 1
Child 1 , pid 57362 : country for group A are Spain Belgium Turkey France
Child 1 , pid 57362 : Teams for group B are Lille Olympiacos Tottenham Bayern
Child 1 , pid 57362 : pots for group B are 4 3 2 1
Child 1 , pid 57362 : country for group B are France Greece England Germany
Child 1 , pid 57362 : Teams for group C are ManCity Valencia Atalanta Shakhtar
Child 1 , pid 57362 : pots for group C are 1 3 4 2
Child 2 , pid 57363 : teams for pot 0 are : Liverpool Arsenal Barcelona ManCity Juventus Bayern Paris Zenit
Child 1 , pid 57362 : country for group C are England Spain Italy Ukraine
Child 2 , pid 57363 : teams for pot 1 are : RealMadrid Atletico Chelsea Dortmund Napoli Shakhtar Tottenham Ajax
Child 2 , pid 57363 : teams for pot 2 are : Benfica Lyon Leverkusen Salzburg Olympiacos Brugge Valencia Internazionale
Child 2 , pid 57363 : teams for pot 3 are : Zagreb Lokomotiv Genk Galatasaray Leipzig SlaviaPraha Atalanta Lille
Child 1 , pid 57362 : Teams for group D are Juventus Leverkusen Lokomotiv Atletico
Child 2 , pid 57363 : Teams for group A are RealMadrid Brugge Galatasaray Paris
Child 1 , pid 57362 : pots for group D are 1 3 4 2
Child 2 , pid 57363 : pots for group A are 2 3 4 1
Child 1 , pid 57362 : country for group D are Italy Germany Russia Spain
Child 2 , pid 57363 : country for group A are Spain Belgium Turkey France
Child 1 , pid 57362 : Teams for group E are Genk Napoli Liverpool Salzburg
Child 1 , pid 57362 : pots for group E are 4 2 1 3
Child 2 , pid 57363 : Teams for group B are Lille Olympiacos Tottenham Bayern
Child 1 , pid 57362 : country for group E are Belgium Italy England Austria
Child 1 , pid 57362 : Teams for group F are Barcelona Internazionale SlaviaPraha Dortmund
Child 2 , pid 57363 : pots for group B are 4 3 2 1
Child 1 , pid 57362 : pots for group F are 1 3 4 2
Child 1 , pid 57362 : country for group F are Spain Italy Czech Germany
Child 2 , pid 57363 : country for group B are France Greece England Germany
Child 1 , pid 57362 : Teams for group G are Leipzig Lyon Zenit Chelsea
Child 2 , pid 57363 : Teams for group C are ManCity Valencia Atalanta Shakhtar
Child 1 , pid 57362 : pots for group G are 4 3 1 2
Child 2 , pid 57363 : pots for group C are 1 3 4 2
Child 1 , pid 57362 : country for group G are Germany France Russia England
Child 2 , pid 57363 : country for group C are England Spain Italy Ukraine
Child 1 , pid 57362 : Teams for group H are Arsenal Benfica Zagreb Ajax
Child 2 , pid 57363 : Teams for group D are Juventus Leverkusen Lokomotiv Atletico
Child 1 , pid 57362 : pots for group H are 1 3 4 2
Child 2 , pid 57363 : pots for group D are 1 3 4 2
Child 1 , pid 57362 : country for group H are England Portugal Croatia Netherlands
Child 2 , pid 57363 : country for group D are Italy Germany Russia Spain
Child 1, pid 57362 : Valid GroupiChild 2 , pid 57363 : Teams for group E are Genk Napoli Liverpool Salzburg
ng
Child 2 , pid 57363 : pots for group E are 4 2 1 3
Child 2 , pid 57363 : country for group E are Belgium Italy England Austria
Child 2 , pid 57363 : Teams for group F are Barcelona Internazionale SlaviaPraha Dortmund
Child 2 , pid 57363 : pots for group F are 1 3 4 2
Child 2 , pid 57363 : country for group F are Spain Italy Czech Germany
Child 2 , pid 57363 : Teams for group G are Leipzig Lyon Zenit Chelsea
Child 2 , pid 57363 : pots for group G are 4 3 1 2
Child 2 , pid 57363 : country for group G are Germany France Russia England
Child 2 , pid 57363 : Teams for group H are Arsenal Benfica Zagreb Ajax
Child 2 , pid 57363 : pots for group H are 1 3 4 2
Child 2 , pid 57363 : country for group H are England Portugal Croatia Netherlands
Child 2, pid 57363 : Valid Grouping
In this program, I fork two processes and the output has a problem that Child 1 interupts and then the Child2 prints its message. Therefore, the "ing" is seperated by one piece of output of Child2.
The sample output should be like
Parent, pid 12352: 2 children, test mode
Child 1, pid 12353: teams for pot 1 are Liverpool Arsenal Barcelona ManCity Juventus Bayern Paris
Zenit
Child 1, pid 12353: teams for pot 2 are RealMadrid Atletico Chelsea Dortmund Napoli Shakhtar
Tottenham Ajax
Child 1, pid 12353: teams for pot 3 are Benfica Lyon Leverkusen Salzburg Olympiacos Brugge
Valencia Internazionale
Child 2, pid 12355: teams for pot 1 are Liverpool Arsenal Barcelona ManCity Juventus Bayern Paris
Zenit
Child 2, pid 12355: teams for pot 2 are RealMadrid Atletico Chelsea Dortmund Napoli Shakhtar
Tottenham Ajax
Child 2, pid 12355: teams for pot 3 are Benfica Lyon Leverkusen Salzburg Olympiacos Brugge
Valencia Internazionale
Child 2, pid 12355: teams for pot 4 are Zagreb Lokomotiv Genk Galatasaray Leipzig SlaviaPraha
Atalanta Lille
Child 2, pid 12355: teams for group A are RealMadrid Brugge Galatasaray Paris
Child 1, pid 12353: teams for pot 4 are Zagreb Lokomotiv Genk Galatasaray Leipzig SlaviaPraha
Atalanta Lille
Child 1, pid 12353: teams for group A are RealMadrid Brugge Galatasaray Paris
Child 1, pid 12353: pots for group A are 2 3 4 1
Child 1, pid 12353: countries for group A are Spain Belgium Turkey France
Child 1, pid 12353: teams for group B are Lille Olympiacos Tottenham Bayern
Child 1, pid 12353: pots for group B are 4 3 2 1
Child 1, pid 12353: countries for group B are France Greece England Germany
. . .
Child 2, pid 12355: countries for group F are Spain Italy Czech Germany
Child 2, pid 12355: teams for group G are Leipzig Lyon Zenit Chelsea
Child 2, pid 12355: pots for group G are 4 3 1 2
Child 1, pid 12353: pots for group H are 1 3 4 2
Child 1, pid 12353: countries for group H are Austria Portugal Croatia Netherlands
Child 1, pid 12353: Valid grouping
Child 2, pid 12355: countries for group G are Germany France Russia England
Child 2, pid 12355: teams for group H are Arsenal Benfica Zagreb Ajax
Child 2, pid 12355: pots for group H are 1 3 4 2
Child 2, pid 12355: countries for group H are Austria Portugal Croatia Netherlands
Child 2, pid 12355: Valid grouping
Here is my code.
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#define LENGTH sizeof(TEAM)/sizeof(TEAM[0])
#define GROUPNUM sizeof(GROUP)/sizeof(GROUP[0])
char *TEAM[] = {"Ajax", "Atalanta", "Atletico", "Barcelona", "Bayern",
"Benfica", "Brugge", "Chelsea", "Crvenazvezda", "Dortmund", "Galatasaray",
"Genk", "Internazionale", "Juventus", "Leipzig", "Leverkusen", "Liverpool",
"Lokomotiv", "Lille", "Lyon", "ManCity", "Napoli", "Olympiacos", "Paris",
"RealMadrid", "Salzburg", "Shakhtar", "SlaviaPraha", "Tottenham", "Valencia",
"Zagreb", "Zenit", "Arsenal"};
char *COUNTRY[] = {"Netherlands", "Italy", "Spain", "Spain", "Germany",
"Portugal", "Belgium", "England", "Serbia", "Germany", "Turkey", "Belgium",
"Italy", "Italy", "Germany", "Germany", "England", "Russia", "France",
"France", "England", "Italy", "Greece", "France", "Spain", "Austria",
"Ukraine", "Czech", "England", "Spain", "Croatia", "Russia", "England"};
char *GROUP[] ={"group A", "group B", "group C", "group D", "group E", "group F", "group G", "group H"};
const char* getCountry(char * team){
int i;
char* str;
for(i=0;i<LENGTH;i++){
str = TEAM[i];
if ( ( strcmp(team, str) ) == 0){
break;
}
}
return COUNTRY[i];
}
int canTheyMeet(char* str1, char* str2){
str1 = getCountry(str1);
str2 = getCountry(str2);
int flag=1;
if ( (strcmp(str1, str2)) == 0 )
flag = 0;
if ( (strcmp(str1, "Ukraine")) == 0 && (strcmp(str2, "Russia")) == 0 )
flag = 0;
if ( (strcmp(str2, "Ukraine")) == 0 && (strcmp(str1, "Russia")) == 0 )
flag = 0;
return flag;
}
// Return Country and It cannot be changed
int whichPot(char * team, char *** result){
int i,j,flag=0;
for(i=0;i<4;i++){
for(j=0;j<8;j++){
char *str = result[i][j];
if( (strcmp(team, str)) == 0 ){
flag=1;
break;
}
}
if(flag==1)
break;
}
return i+1;
}
int isGroupValid(char ** group, char *** result){
int i,j;
int flag = 1;
for(i=0;i<3;i++){
for(j=i+1;j<4;j++){
char *str1 = group[i];
char *str2 = group[j];
if(!canTheyMeet(str1, str2) || whichPot(str1, result) == whichPot(str2, result)){
flag=0;
break;
}
}
}
return flag;
}
// only for test mode, we generate the Group for each teams
char *** generateGroup(char** argv, int length){
int m;
char *** result;
result = (char ***)malloc(sizeof(char**)*8);
for(m=0;m<8;m++){
result[m] = (char**)malloc(sizeof(char*)*4);
}
int k,j;
int i=35;
for(k=0;k<8;k++){
for(j=0;j<4;j++){
char* string = argv[i + j];
result[k][j] = string;
}
i+=4;
}
return result;
}
// set Teams to different pots
// The pots are three dimensional Array
char *** getTeams(char** argv, int length){
int m;
char *** result;
result = (char ***)malloc(sizeof(char**)*4);
for (m=0;m<4;m++){
result[m] = (char**)malloc(sizeof(char*)*8);
}
int i=3;
int j=0;
int k=0;
//Iterative get all the information of setmode
for(; k<4; k++) {
// four pots
for (; j < 8; j ++) {
// get string
char *string = argv[i + j];
result[k][j] = string;
}
i+=8;
j=0;
}
return result;
}
void TestMode(char ** argv, int length, int id){
char *** teamPot = getTeams(argv, length);
char *** groups = generateGroup(argv, length);
int i,j,validBit=1;
for(i=0;i<4;i++){
printf("Child %d , pid %d : teams for pot %d are : ", id, getpid(), i);
for(j=0;j<8;j++){
printf("%s ", teamPot[i][j]);
}
printf("\n");
}
for(i=0;i<8;i++){
printf("Child %d , pid %d : Teams for %s are ", id, getpid(), GROUP[i]);
for(j=0;j<4;j++){
printf("%s ", groups[i][j]);
}
printf("\n");
printf("Child %d , pid %d : pots for %s are ", id, getpid() ,GROUP[i]);
for(j=0;j<4;j++){
printf("%d ", whichPot(groups[i][j], teamPot));
}
printf("\n");
printf("Child %d , pid %d : country for %s are ", id, getpid() ,GROUP[i]);
for(j=0;j<4;j++){
printf("%s ", getCountry(groups[i][j]));
}
printf("\n");
char ** group = groups[i];
if(!isGroupValid(group, teamPot)){
printf("Child %d, pid %d : InValid Grouping\n" , id, getpid());
validBit=0;
break;
}
}
if(validBit)
printf("Child %d, pid %d : Valid Grouping\n", id, getpid());
free(teamPot);
free(groups);
}
void GenerateMode(){}
int main(int argc, char **argv) {
printf("Hello, World! %d\n", GROUPNUM);
int k=0;
int status=0;
// get number of child process
int numOfChild = 2;
// The parent Id
pid_t ppid = getpid();
printf("Parent, pid %d : %d children %s Mode \n", ppid, numOfChild, argv[2]);
pid_t pid;
for(k=0;k<numOfChild;k++){
if(fork() == 0){
if ((strcmp(argv[2], "T") == 0)){
TestMode(argv, argc, k+1);
exit(0);
}else if ((strcmp(argv[2], "G")) == 0){
GenerateMode();
}
}
}
for(k=0;k<numOfChild;k++){
wait(NULL);
}
return 0;
}
Do I need to do some jobs with Lock?
Thanks!!!
All forked processes are writing to stdout asynchronously. There are no guarantees about order. Calling setlinebuf (equivalent to setvbuf(stream, NULL, _IOLBF, 0)) once at start of the program and fflush after each printf should help, but will not necessarily solve your problem in all cases.
Specifying a buffer size larger than your anticipated longest line when calling setvbuf will also help:
char buffer[16000]; // 16000 is the buffer size in bytes
...
int main(int argc, char **argv)
{
setvbuf(stdout, buffer, _IOLBF, sizeof buffer);
...
Related
Extract items from Column in a Dataframe
country_name rank show_title Argentina 2 The Queen of Flow India 1 Cobra Kai Argentina 1 The Queen of Flow England 3 Stay Close Argentina 1 The Queen of Flow I am trying to get a table that will display the number of times each show title is ranked 1st, 2nd or Third. The result something like this: Rank Cobra Kai The Queen of Flow Stay Close 1 1 2 0 2 0 1 1 3 0 0 0
You can use pivot_table like this. df.pivot_table(index=['rank'], columns=['show_title'], aggfunc='count', fill_value=0) Result country_name show_title Cobra Kai Stay Close The Queen of Flow rank 1 1 0 2 2 0 0 1 3 0 1 0
how to scanf multiple values into a 2d array using only one command which varies with user input
I am attempting to create a minesweeper game whereby the user can choose how many mines to enter, list their co ordinates in a row x column manner at whic point they will be inserted into an array.The program should act as below. However I am unsure as to how I am going to scanf multiple co ordinates into an array. The code I have acquired thus far is available below. Welcome to minesweeper! How many mines? 3 Enter pairs: 0 0 1 1 Game Started 2 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 int number_mines; int x; int y; printf("Welcome to minesweeper!\n"); printf("How many mines? "); scanf("%d\n", &number_mines); // TODO: Scan in the number of pairs of mines. printf("Enter pairs:\n"); scanf("%d %d\n", &x, &y); printf("Game Started\n"); for(int i = 0; i < 8; i++){ for(int j = 0; j< 8; j++){ if (x == i || y == j){ printf("2"); } else{ printf("1"); } } }
Concurrent Processes using fork()
I am given the following code: main() { int i, rc; for (i = 0; i<=1; i++) { if( (rc=fork()) == 0) { printf("Child %d executing\n",i); } /*end if*/ } /*end for*/ } printf("All children created\n"); I am also given the solution of the possible permutations of which outputs may occur. Child 0 executing | Child 1 executing | Child 1 All children created | Child 1 executing | Child 2 All children created | Child 1 executing | Grand child All children created | All children created | Parent I know that these outputs are created by each process but I am just having trouble tracing them to understand HOW these outputs occur. I know fork() creates a process and if (fork() == 0) means it is a child process, but if anyone can help me understand where the answers beyond Child 0 executing | come about thanks. I believe the | is just a description of which process is currently being ran. How come child 1 can create a "grand child" but child 0 can not?
First, the code and behavior will be easier to understand if the loop is unrolled. The code then becomes: int rc; if ((rc = fork()) == 0) printf("Child 0 executing\n"); if ((rc = fork()) == 0) printf("Child 1 executing\n"); printf("All children created\n"); Then, to help understand what's going, the best is to draw the process hierarchy as a tree. Here is an ASCII version of it: main /| / | / |\ child0 | \ | | \ | | child1 /| | | / | | | / | | end / end | / | child1 end | | end In the graph, child0 is the printf statement displaying "Child 0 executing", child1 is the statement "Child 1 executing" and "end" is the printf statement displaying "All children created". As you can see from the graph, you'll get 1x child0, 2x child1 and 4x "All children created". UPDATE #bkennedy Here is another view that shows the process view only, with P0 being the main (original) process and "end" indicating each process' completion: P0 /| / | / |\ P1 | \ | | \ | | P2 /| | | / | | | / | | end / end | / | P3 end | | end There are really 4 processes: P0 (main), P1, P2 and P3. P1 is the first child of P0; it displays "Child 0 executing". P2 is the second child of P0; it displays "Child 1 executing". P2 never creates any children, it just finishes with the printf statement. P3 is the first (and only) child of P1. Each process displays "All children created" when they finish. Remember: P0 (main) goes through 2 fork calls, hence the 2 children. P1 goes through 1 fork call, hence the single child (P3). P2 never goes through a fork call. That's it, there is no other process creation. I'm not sure how to explain this better to you.
After the first fork and the if statement that it's in, the "child 0 executing" process and its silent parent process main go on to the next if statement. There they both produce a "child 1 executing" process while remaining silent. All of those four processes, ie main, the "child 0 executing" process, main's "child 1 executing" process" and the "child 0 executing" process's "child 1 executing process", go on after the second fork and the if statement that it's in to print "all children created". One of "Child 0 executing" is output, two of "Child 1 executing" are output, and four of "All children created" are output. How come child 1 can create a "grand child" but child 0 can not? No "child 1" creates a child. Main and "child 0" each create a "child 1". The output apparently calls "child 0"'s "child 1" the "Grand child" of main. So no "child 1" creates a child, and both main and "child 0" produce a "child 1". So your question is wrong about both "child 1" (of which there are two) and "child 0". HOW these outputs occur The quoted "output" seems to call main Parent, the "child 0" process Child 0, main's "child 1" process Child 2 and the "child 0" process's "child 1" process Grand child. So you need to distinguish between what the "output" is naming the processes and what those processes are outputing. It also has three lines outputing "Child 1 executing", which is wrong. But you should be telling us what that "output" is supposed to be.
struct elements erasing itself
Ok so I am reading some things from files with this code: for (i=0; i<start;i++) { filename=files[i]; if((fp=fopen(filename, "r"))==NULL) { printf("unable to open %s\n", filename); exit(1); } while(fgets(buffer, sizeof(buffer), fp) !=NULL) { d[counter].id=atoi(strtok(buffer, del)); strcpy(buffer2, strtok(NULL, del)); len=strlen(buffer2); if(buffer2[len-1]=='\n') buffer2[len-1]='\0'; strcpy(d[counter].name, buffer2); counter++; } token = strtok (filename, del1); holder=token; token = strtok (NULL, del1); /* section*/ token2 = strtok(holder, del2); token2 = strtok(NULL, del2); /*course name */ for(x=z;x<counter;x++) { d[x].section=atoi(token); printf("%d ", d[x].section); strcpy(d[x].course, token2); printf("%s %d %s %d\n", d[x].course, d[x].section, d[x].name, d[x].id); } z=counter; } Struct definition: struct student { char course[8]; int section; char name[19]; int id; }; Everything prints fine except for the "section" in the struct, for some reason the elements makes itself 0 after a certain amount. Here is the output: 1 CSE1325 1 Sally 3233 1 CSE1325 1 George 9473 2 CSE1325 2 Tom 1234 2 CSE1325 2 Ralph 3540 2 CSE1325 2 Mary 5678 1 CSE2312 1 Tom 1234 1 CSE2312 1 Ralph 3540 1 CSE2312 1 Mary 5678 1 CSE2315 1 Tom 1234 1 CSE2315 1 Ralph 3540 1 CSE2315 1 Mary 5678 2 CSE2315 2 Sally 3233 2 CSE2315 2 George 9473 4 ENGL1301 0 Tom 1234 4 ENGL1301 0 Sally 3233 4 ENGL1301 0 Ralph 3540 4 ENGL1301 0 Mary 5678 4 ENGL1301 0 George 9473 1 HIST1311 0 Tom 1234 1 HIST1311 0 Sally 3233 1 HIST1311 0 Ralph 3540 1 HIST1311 0 Mary 5678 1 HIST1311 0 George 9473 5 MATH1426 0 Sally 3233 5 MATH1426 0 George 9473 This is the expected output: 1 CSE1325 1 Sally 3233 1 CSE1325 1 George 9473 2 CSE1325 2 Tom 1234 2 CSE1325 2 Ralph 3540 2 CSE1325 2 Mary 5678 1 CSE2312 1 Tom 1234 1 CSE2312 1 Ralph 3540 1 CSE2312 1 Mary 5678 1 CSE2315 1 Tom 1234 1 CSE2315 1 Ralph 3540 1 CSE2315 1 Mary 5678 2 CSE2315 2 Sally 3233 2 CSE2315 2 George 9473 4 ENGL1301 4 Tom 1234 4 ENGL1301 4 Sally 3233 4 ENGL1301 4 Ralph 3540 4 ENGL1301 4 Mary 5678 4 ENGL1301 4 George 9473 1 HIST1311 1 Tom 1234 1 HIST1311 1 Sally 3233 1 HIST1311 1 Ralph 3540 1 HIST1311 1 Mary 5678 1 HIST1311 1 George 9473 5 MATH1426 5 Sally 3233 5 MATH1426 5 George 9473 See how the numbers match? But for mine it doesn't, when I print d[x].section in the for loop as a standalone it prints the correct thing, but when I use it in that combined print statement for some reason it prints out 0 when reaching ENGL1301.
The course number can be eight characters, and strings in C are by convention null-terminated. Since your declaration is char course[8], when there are eight characters in the course number, the terminating null is being put off the end of course which lands in the section number, making it zero. Declaring char course[9] should do the trick.
The 0 is almost certainly the null terminator from this value (and the other values of the same length): "ENGL1301" This 8 character string is 9 characters when you include the null terminator at the end of the string. In this case, the null terminator is being written past the end of the string, which happens to be where the section is stored. To fix this, declare course as char course[9]
printing a periodical series of a number in C
Given a Periodical Series of numbers Example X=3 the Periodical Series of x=3 must look like this 1 1 1 1 1 2 1 1 3 1 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 2 1 1 2 1 2 2 1 3 2 2 1 2 2 2 2 2 3 2 3 1 2 3 2 2 3 3 3 1 1 3 1 2 3 1 3 3 2 1 3 2 2 3 2 3 3 3 1 3 3 2 3 3 3 Wanted: to write a program in C to print this series and given : Assume maximum value of x can be 10 I tried to start with ideas.. but all failed.. Please HELP . thanks :)
def yourFunction(n, x): recursiveFunction(n, x, 0, [0]*n) def recursiveFunction(depth, breadth, currentDepth, indexes): if currentDepth >= depth: print indexes else: for indexes[currentDepth] in range(0, breadth): recursiveFunction(depth, breadth, currentDepth + 1, indexes)
#include<stdio.h> #include<stdlib.h> int printSeries(int list[], int max, int level){ if(level == max){ int i; for(i=0;i<max;++i) printf("%d ", list[i]); printf("\n"); return 1; } while(list[level]<=max){ list[level]+=printSeries(list, max, level+1); } list[level] = 1; return 1; } void printSeriesStart(int x){ int i, *list = malloc(x*sizeof(int)); for(i=0;i<x;++i){ list[i]=1; } printSeries(list, x, 0); free(list); } int main(void){ int X = 3; printSeriesStart(X); return 0; }