@moroccanplaya
Gerard is opening the file in r mode while you are opening the file in rb mode. Could this be an issue ?
@moroccanplaya
Gerard is opening the file in r mode while you are opening the file in rb mode. Could this be an issue ?
Some pseudo code
while(true)
{
// Take the I/P from the user
// Check if the first row has all X's
// Check if the second row has all X's
....
...
// CHeck for all rows, all columns, diagonally for X's and 0's
}
Also you cannot use variables as case numbers in a switch statement. It has to be to contants
Check out this link
Just a thought ... I think this might help you ...
When you use a file for storing data you cannot just update a single line of text in that file. You have to read the entire file into memory, make the changes that you want in the memory and then write out the modified memory back into the file
Hello Everybody,
I have a small confusion with regards to pointers. Here is some code
int main()
{
int *p1 = (int*)malloc(sizeof(int));
*p1 = 10;
printf("The number is %d",*(char*)p1);
}
I run the code and the O/p is 10. My question is, is there no difference in using char typecast as opposed to an int typecast ?
Quick question ... Is the third parameter in the equal function a default parameter ?
This is your function f1
void f1()
{
// Your code
return 0; // Functions which have a return type of void cannot return a value
}
If you dont want to implement your own sorting function then check out this link.
PS I know this link is from a C++ reference, but I think it will work in c code as well. Others please correct me if I am wrong
You should first try to write a string matching algorithm .
If the longer strings contains the smaller string in many places, can you successfully print the locations of the sub string in the main string ?
The error on line 52 is that you cannot use string as a name of a variable ... I think the keyword string is reserved. If you notice it got high lighted when you pasted the code here
Also why are you doing an operation inside the print function ?
Do something of this sort for inducing delay
1. Create a thread to run in the background. Let this thread call function f1() when it wakes up.
int flag = 0;
int main()
{
// Create a thread to run in the background. Let this thread call f1()
int numberOfThreads = -1;
printf("Enter the number of threads\n");
scanf("%d",&numberOfThreads);
int flag = 1;
return 0;
}
void f1()
{
// Write this code. In addition to this also check if flag is set to 1. If flag is set to 1 then break out of the while loop. If flag is not set to 1 till the loop ends then exit the main program
}
There are 2 concepts involved here
Concept 1: In a Binary Search tree if you print all of the nodes inorder then the output will be an array sorted in ascending order. Extending this to your problem, you can use the last name as an index for inserting data into the bst tree. Then when you print the data it will be sorted in ascending order by last name.
Concept 2: A binary search tree is basically a collection of structs. You can store any number of names in a struct. So you struct could be
struct person
{
char* firstName;
char* lastName;
char* middleName;
};
Disclaimer: If you want to sort your BST by first name, last name, middle name simultaneously then the problem is a hard problem
In the if ... else block you have used the variable total twice. I think you meant to use totalPoints in one of the places ...
Also if I am using 2 conditions separated by a logical operator in an if statement trying I write it like this
if ( (a <100) && (a >50))
{}
This makes it easy for me to spot an error in my conditions
Some of the functions you might need
strtok --> Use this function to split a string into a series of words
strcmp --> Check if this word is a name or a period
@rohan
I think, theoretically speaking the approach described by Walt is faster than your approach
Walt's Approach
Read the entire file to find the size of the file --> O(n)
Alloc a buffer of size n and copy the contents of the file in buffer --> O(n)O(n) + O(n) = O(n)
Your approach
while(contents of file exist)
{
Read 1 line of file --> O(1) Say
Copy the contents of the buffer into new buffer ---> O(n)
}
This means theoretical complexity of O(n)*O(n) = O(n^2)
If you want to make your own string comparison function
1. If length of string 1 is not equal to string 2 then, strings are not identical
2. for i:0, to strlen, check if the str1 == str2. If they are not equal then break out of the loop
String reverse without using additional space
1. Use strlen (or write your own function) to calculate the length of the string
2. x= strlen /2
3. from 0 to x, interchange the positions of the first and last, second and second last .... and so on
Is there any max size to the titles ?
Is there any way to determine how many titles are going to come in ?
Instead of a 2d array how about a linked list of structs ?
In your while loop (lines 36 - 39) you are not checking to see if you hit NULL or not
WHen you do current = current->next check to see current->next is not equal to NULL
I really like "Let us C" by Yashwant Kanetkar
This book assumes no previous knowledge of programming and teaches C programming in a very step step and methodical manner
This is how I would approach this problem
Suppose I have to remove the string "tes" from United States of America
1. Check to see if I can find a t any where in United states of America
2. If do find a t then check for e and then s
3. Once I find the start index of the match, in this case 10 the end index has to be 13
4. Then from index 14 till the end of the string I copy chars one by one to positions 10,11,12 ....
I guess the tricky part of this exercise is identifying when you have a string match. Check out this algorithm. Might be of help
This gives general information about bots
I am not entirely convinced if you need this information for destructive or constructive purposes so I will not give any further information. If you are really interested in this topic then google will be your best friend in this endeavor
Do a google search for "implement my own strstr function" you will find lots of suggestions
What kind of error are you getting ?
Did you include all the header files ?
Check out this piece of code
int
main()
{
char arr[10]; //Max i/p size is 10 ie 9 letter and \0
char* p = NULL;
printf("Enter your name \n");
fgets(arr,10,stdin) ; //stdin is standard input
// If the user enters 9 or less chars then the first 10 chars will definitely have a \n .Otherwise there wont be a \n in the first 10 chars
if((p = strchr(arr,'\n')) != NULL)
arr[p - arr] ='\0';
else
arr[9] = '\0';
printf("Your name is %s\n",arr);
return 0;
}
Dont use fscanf use fread .
Read the input into a char buffer . Then use string processing to extract the information you need
If you want to use pointers to pass a 2-D array write your code like this
void
printStuff(int* a,int rows,int cols)
{
int i=0,j=0;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
printf("%d\t",a[(i*rows) + j]);
}
}
int
main(int argc, char *argv[])
{
int a[3][3];
int i=0,j=0,count=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
a[i][j] = count++;
}
printStuff(&a[0][0],3,3);
return 0;
}
You could take the hash map route
What have you got so far ?
How do you plan to approach this problem ?
What have you got so far ...
How do you plan to approach this problem ?
Is the format of the input file pre defined or can it change
If the input file format is not fixed then this is a hard problem
Write your accept function this way
I have separated the data input and the smallest number calculation into 2 steps but you dont have to
void accept(int *a)
{
int size =0,i=0;
printf("Enter the size of the array\n");
scanf("%d",&size);
a = (int*)malloc(sizeof(int)*size); // Size of the i/p array is dynamic
for(i=0;i<size;i++)
{
printf("Enter the next integer\n");
scanf("%d",&a[i]);
}
printf("The entered numbers are \n");
for(i=0;i<size;i++) // Just for checking
printf("%d\n",a[i]);
}
int
smallest(int* a)
{
// Logic for finding the smallest number
return 0;
}
int
main(int argc, char *argv[])
{
int *a;
accept(a);
printf("The smallest number is %d\n",smallest(a));
free(a);
return 0;
}
Hi I modified the above program some what ...
I tested the program for /bin/ls and /bin/pwd and the program worked. The program makes some assumptions. I will leave them for you to clear out :)
int
main()
{
char temp[256];
char *argv[100];
char *p;
int i=0;
int child;
while(1)
{
printf("\n\n$ ");
fgets(temp,256,stdin);
{
p = strchr(temp,'\n');
temp[p-temp] = '\0';
argv[i] = strtok(temp," ");
while(argv[i] != NULL)
{
printf("%s\n",argv[i]);
i++;
argv[i] = strtok(NULL," ");
}
printf("Before fork\n");
child = fork();
if(child < 0)
{
printf("Error\n");
exit(0);
}
if(child == 0)
{
printf("In child\n");
if(execve(argv[0],argv,0) == -1)
printf("Execve error\n");
}
if(child > 0)
{
}
}
}
return 0;
}
atoi is used with string and not chars. This link explain the function along with some sample code
just google for malloc/ calloc you will find tons of documentation and examples
Also why dont you guys write your code incrementally ?
Then if you get a compiler error, it is quite easy to spot the reason
When you are reading from the file the value of MAX is 101. But each line has less than 101 characters . So what you want to do is something of this sort
int
main()
{
FILE *fp = fopen("mytestfile.txt","r");
char arr[100]; // Open a file
char *p;
if(fp == NULL)
{
printf("File does not exist");
return -1;
}
while(fgets(arr,100,fp)!=NULL) // Read from the file
{ // Assume each line will have less than 100 character
p = strchr(arr,'\n'); //Each line will be terminated by \n. Find the position of the \n
arr[p - arr] = '\0'; // Replace the \n by \0
printf("%s\n",arr);
strcpy(arr,"\0"); // Clear the array
}
fclose(fp);
return 0;
}
Use the code tag..
After you have pasted your code into the little box, select all the code and then click on the (CODE)
Preview your post to see if the formatting has come out correctly or not
Xcode hides lots of stuff from you. If your are going to be using C only for a couple of days then probably X code is the right way to go. But if you want to learn the language from ground up, then probably you want to explore the terminal option
Simplest solutions: Make the variable global . So you want to write some thing of this sort
char *p = NULL;
void func()
{
p= "Value returned using global variables";
}
int
main(int argc, char *argv[])
{
func();
printf("%s\n",p);
return 0;
}
Or you can return a char array from a function. Then you are writing code of this sort
char* func()
{
char *p = (char *)malloc(sizeof(char)*10);
p[0]='A';
p[1]= 'B';
p[2]= 'C';
return p;
}
int
main(int argc, char *argv[])
{
char* p =func();
printf("%s\n",p);
free(p);
return 0;
}
Try solving the unsolved questions on this forums ...
Or check out source forge ... Its full of exciting projects and they always need more contributors there
malloc memory for the list pointer before sending it into the function
I have added my suggestions as comments to your code
if you ask me would code it like this:
#include <stdio.h> #include <stdlib.h> struct node { int value; struct node *next; }; typedef struct { struct node *first; } LLIST; //Why do you need this struct int list_add(LLIST *list, int n) { struct node *item; int i; for(i = 0; i < n; i++) { item = malloc(sizeof(struct node)); if (!item) return -1; item->value = i; item->next = list->first; // In the first iteration list will be NUL. Attempt to dereference a NULL pointer will give a seg fault list->first = item; } return 0; } void list_del(LLIST *list) { struct node *item; while ((item = list->first)) { list->first = item->next; free(item); } } int main(void) { LLIST list = { NULL }; struct node *item; if (list_add(&list, 10)) return 1; for (item = list.first; item; item = item->next) printf("value=%d\n", item->value); list_del(&list); return 0; }
Yes I know the thread is almost a year old but this seemed like an interesting questions and there was no follow up
@winrawr I ran your code in a Windows machine and it worked. No seg fault no nothing
aha :) thanks man :) i get it :)
and last question...
Do you think that this is the optimal way for linked list or ? Can you give some advice :)
I dont think the code you have written is correct. What do you wan to do in the add function ? Add to a singly linked list or circularly linked list ?
LLIST *list_add(LLIST **p, int i)
{
if (p == NULL) // This should be *p
return NULL; // If the head of the list in NULL then you have to create a head. If you return then your linked list will never get created
LLIST *n = malloc(sizeof(LLIST));
if (n == NULL)
return NULL;
n->next = *p; /
*p = n; // What do you want to do here ?
n->data = i;
return *p;
}
It would be more helpful if you gave me some more information about the "PROBLEM" you are having ?
How about using some debugger/ printfs and trying to pin point your problem
Just saying program does not work wont cut it.
Quick question why are create_node and add_tree separate functions ?
All you are doing in create_node is allotting memory for a new node and then checking to see if this node should be the parent node in the tree or not ?
Maybe you want to combine both of these functions. Here is how I would do it .
void add_Node(struct node** parent, int value)
{
// Allocate new memory. Check for error
// Check if *parent is NULL. If it is then this is the first node in the tree. Else this is a child
// If this is the first node in the tree then you are done
// Else start a while loop for to figure out where exactly in the tree should this new node be placed
}