Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
So you are to read in integers c and m. C is the number of cases, m is the number of lines per case. After m, there are m lines containing any number of letters, no doubles (a-z). After those lines there is an int r, which is the solution number we want if we did recursion for every possible result having one letter from each line, forming a string.

There is a way to skip recursion, and go straight to the rank (int r) that we want. I am having a problem with my pointers by trying to send my struct into functions. all my errors pertain to my struct and sending my struct into my functions. Any help would be great. Here is a sample of my code.

#include <stdio.h>
#include <stdlib.h>
#define MAX 26
//Struct to hold all information inside
struct passwordT{
char letters[MAX];
int charCount;
int possibleSkips;
int solutionArray;
};

//Functions. numOfSkips gets number of solutions to skip for each line, and getSol gets the array for the  
lines of chars.
passwordT* numOfSkips(passwordT *T, int *total, int m);
int getSol(int ps, int v, int m, int r);



int main(){
int i, j, k;
int c, m, r;
char p;
int total = 0;

//Read in Number of cases
scanf("%d", &c);

//Declaring a pointer to struct passwordT, then mallocing for the size of the number of cases
passwordT* ptrToPass;
ptrToPass = malloc(c*sizeof(passwordT));

//Cycle through cases
for (i=0; i<c;i++){

    //Read in number of lines
    scanf("%d", &m);

    //Reading in the string on each line
    for(j=0;j<m;j++){
        scanf("%s", &ptrToPass[j].letters);
    }

    //Get number of characters for each string
    for(j=0; j<m; j++){
        ptrToPass[j].charCount = 0;
        k=0;
        p = ptrToPass[j].letters[0];
        while(p!='\0'){
            ptrToPass[j].charCount ++;
            k++;
            p = ptrToPass[j].letters[k];
        }
    }

    //Scan in solution number wanted
    scanf("%d", &r);

    //Get number of solutions to skip
    ptrToPass = numOfSkips(ptrToPass, &total, m);

    //Get solution set
    for(j=0;j<m;j++)
        ptrToPass[j].solutionArray = getSol(ptrToPass[j].possibleSkips, 1, m, r);

    //Print results
    for(j=0;j<m;j++)
        printf("%c", ptrToPass[j].letters[ptrToPass[j].solutionArray]);
}

return 0;
}

//This struct is for an algorithm used to skip to int r without recursion
passwordT* numOfSkips(passwordT *T, int *total, int m){

passwordT *skippers;
int i = 0;

//If i = 0, possible solution skips is 1. Else, possible solution skips for skippers[i] is total. Then total = 
total times current lines char count
for(i=0;i<m;i++){

    if(i==0){
        skippers[i].possibleSkips = 1;
        total = skippers[i].charCount;
    }

    else{
        skippers[i].possibleSkips = total;
        total = total * skippers[i].charCount;
    }
}
}


// Function used to get the array of the solution (int r) would be if we used recursion
int getSol(int ps, int v, int m,int r){
int sa = 0;
int i;
for(i=0; i<m; i++){
    while(v <= r - ps){
        sa ++;
        v += sa;
    }
}

return sa;

}



Here are my error lines

VB
12: syntax error before '*' token (Declaration of first function numOfSkips in header
13: warning, data definition has no type or storage class

IN MAIN

28: passwordT undeclared, first use in this function ( so when i have passwordT* ptrToPass;)
28: ptrtoPass undeclared

IN numOfSkips

73: syntax error before '*'--> error with passwordT* numOfskips(passwordT *T, int *total, int m){
79: m undeclared (first use in this function) --> First time I use m in function numOfSkips
83: total undeclared (same situation as line 79)
Posted
Comments
Sergey Alexandrovich Kryukov 19-Sep-13 11:59am    
Look, if you show error messages by line numbers, you could show those line numbers in your code sample... Why do you want people to solve puzzles?
—SA
Jochen Arndt 19-Sep-13 12:03pm    
The line with 'lines of chars.' should be a comment line with leading '//'. Does this avoid the error?
Sergey Alexandrovich Kryukov 19-Sep-13 13:19pm    
I noticed that, but I thought it was only the artifact of code formatting...
—SA
Jochen Arndt 19-Sep-13 13:24pm    
It may be a pasting / formatting error. So I asked if this solves the problem.
But it is after line #12 and would explain the errors.
Sergey Alexandrovich Kryukov 19-Sep-13 13:32pm    
Agree.
—SA

1 solution

As Jochen already mentioned you forgot to put comment characters in front of some of you comment lines.

The error in line 28 result from declaring the pointer ptrToPass after an executable statement. Remember, in C all declarations must be at the top of a function!

In your numOfSkips function you also forgot to comment out some lines. Also: Pointer skippers is declared and used, but never assigned to. That will definitely lead to a crash.

Your numOfSkips function does not return a value, although it is declared to return a passworT pointer.

And finally a couple of recommendations:

- Use proper indentation in your code; as it stands it is hard to read as everything inside your functions is flush left.

- Read your program and put yourself in the role of the processor. Then go through your code step by step and see if it will do what you expect.

- When running the program the first time, use a debugger and step through your program and again compare the state of variables with what you expect them to contain. This is probably the most important advice: Learn to use the debugger!

Good luck!
 
Share this answer
 
Comments
H.Brydon 19-Sep-13 17:43pm    
+5 for the effort
nv3 20-Sep-13 2:14am    
Thanks, Harvey!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900