csurfer 422 Posting Pro

After the end of file,the file is filled with null characters,so if you move the file pointer to a far off place and get a line from there then it inputs several '\0' characters into your string which when output by << doesn't show anything.

NULL is a macro for (void *)0 called null pointer and its completely different from '\0' which is null character with ascii value 0.

csurfer 422 Posting Pro

It would be better if you divide that nary tree nodes in some hierarchical levels so that it would be helpful in deciding their positions in future.

csurfer 422 Posting Pro

Hey its quite easy , 0x is hex right then ABC is calculated as follows :
A * 16^2 + B * 16^1 + C * 16^0 i,e
10 * 484 + 11 * 16 + 12 * 1
2748

csurfer 422 Posting Pro

This is where you went wrong.

In your program pointers min and max are there but are not pointing to any memory space at all.So when you try to access some memory as *min or *max they try to refer a memory not been allocated hence segmentation fault core dumped.

Try this:

int main( int argc, char *argv[] ){

int *numberlist, i, n, *min, *max ,minimum,maximum;

min=&minimum;
max=&maximum;
//Followed by your code

By this you will have allocated the memory and then accessing it so no problem. And ya your max and min values will be in maximum and minimum.

csurfer 422 Posting Pro

You have used %i every where for scanning integer variables as:

scanf("%i", &n);

this is wrong the correct usage is,

scanf("%d", &n);

You need to correct this all throughout the program, and ya before calling the search function you need to call a function to sort the inputs and I didn't see any such function in your program.And I hope your search calling function is correct ;).

csurfer 422 Posting Pro

I think the conversation is loosing track. You want to write a C program to write main function,take input in array and then sort it through some sorting algorithm and then run search through your assembly program right? Concentrate on that.Do this first:

1)Writing a main program and taking inputs into array should be simple as it is very much similar to JAVA programming the same thing.Do it first.

2)You will get all the help needed for sorting algorithms form this community threads and also the link
http://en.wikipedia.org/wiki/Sorting_algorithm

3)If you want it still simplified here you get algorithm and modules to write bubble sort and also simulated explaination.
http://www.cs.princeton.edu/~ah/alg_anim/gawain-4.0/BubbleSort.html

4)After this concentrate on making it search for the user input in the array through your assembly code.

Don't loose your way.Concentrate on what path to take to make compiler do your work.Then you can code anything.

csurfer 422 Posting Pro

What good would strstr do after you have tokenized the string???

Declare a string array and an integer array of some respectable size.

Tokenize the string and assign every string formed to the indexes of string array.Now use the string array indexes and strcmp to find the repetition of a particular string and store the number of repetitions in the integer array and initialize all the repeated strings to null every time you use strcmp.

csurfer 422 Posting Pro

I really think you need to relax your brain because you are going wrong again in this :

do{
printf("Would you like to place another order? (Y/N)");
scanf("%c",&end);
end = toupper(end);
}while (end != 'Y' && end != 'N');

Here the do while loop only works if you press in some garbage value or character apart from "y" and "n".That is not what you want.Just change it to

while(end!='N')

I don't see any problem with the toupper call. Just the above change is enough and I haven't gone through other parts of your program.

csurfer 422 Posting Pro

Thanks for helping me guyz, my second problem is about the character "end".

i used toupper but it's not working..

do{
	printf("Would you like to place another order?  (Y/N)");
	scanf("%c",&end);

	end = toupper(end);

	}while (end != 'Y' || end != 'N');

T_T

while (end != 'Y' || end != 'N');

This statement is a complete nonsense...If end is equal to Y you need to loop again right? This thing doesn't do anything...
Atleast do this :

while (end == 'Y' || end != 'N');

or even this:

while (end == 'Y' );

And it involves the header file "ctype.h"

csurfer 422 Posting Pro

>theMenu[0].menuItem[LENGTH]=" [1] Chicken A";
That won't work either. You'll have to strcpy the strings into the character arrays, or point each node of menuItem to a string literal and then make menuItem a const char pointer (to make sure that you don't accidentally try to modify the string literal).

Ya a mistake on my part sorry I got it confused with

char arr[30]="Hello I am .....";

But that cannot be done on the later part I mean after initialization so best method is as you suggested "strcpy". This is the reason I love this forum.I get a chance to learn a new thing and also learn from my mistakes every now and then.Every one please note "A small mistake on one topic done by you will make this forum teach you two topics.Thanks Daniweb!!!"

csurfer 422 Posting Pro

Make these changes in getData function:

int getData(int);

change this to

int getData();

-------------------------------------------------

choice = getData(int);

change this to

choice = getData();

-------------------------------------------------

int getData(int z)
{
	z=0;

change this to

int getData()
{
	int z=0;

----------------------------------------------------------------------------
----------------------------------------------------------------------------
You cannot do this in menu function:

strcpy(theMenu[y].menuItem[LENGTH],{	" [1]  Chicken A",
					" [2]  Chicken B",
					" [3]  Chicken C",
					" [4]  Chicken D",
					" [5]  Chicken E",

You need to do it separate initialization only for both the arrays as:

theMenu[0].menuItem[LENGTH]=" [1]  Chicken A";
theMenu[1].menuItem[LENGTH]=" [2]  Chicken B";
theMenu[2].menuItem[LENGTH]=" [3]  Chicken C";
theMenu[3].menuItem[LENGTH]=" [4]  Chicken D";
theMenu[4].menuItem[LENGTH]=" [5]  Chicken E";
//continues
csurfer 422 Posting Pro

plz help me to add two matrices using pointers..

1>Take two integer matrices say a[20],b[20],and a result matrix c[20],and two integer pointers pa and pb.

2>Assign the start address of arrays to these pointers.

3>Now if p points to a[0] then p+1 points to a[1] and you can even access it as *(p+1) which is equal to a[1].

4>By the above mentioned process go on adding two matrices by using *(p+i) and store it in c.

csurfer 422 Posting Pro

Hey there are few errors which you can work on:

1)

void chkEClass(.........................)
{
}

Has an error in first line as:

void chkEClass( , , , ,char firstname[],char lastname[])

this should be

void chkEClass( , , , ,char firstname[][LENGTH],char lastname[][LENGTH])

2)There exists an error in the looping concept while taking the input which exits the program even when the user hasn't finished his choices.

Go on through the code stage by stage as though you are compiling it you will find out the errors more clearly.

bemo55 commented: You were helpful. All it took was for me to take take to stop and look at my code line by line +1
csurfer 422 Posting Pro

>gets(char);
FYI, booze in fact does not improve your ability to write code. You'd have to be quite confused to write char when you meant firstname, and any experienced C programmer would have to be drunk to use gets.

Ya that was firstname. Sorry mis-representation. Call it a "Slip of keys". ;)

csurfer 422 Posting Pro

I have a project for school that i am working on, and i cant figure out how to store names in an array. i have it declared as

char firstname[25];
char lastname[25];

but i was told that just stored one string and when i go to print out the names that are input, it just prints the last one i put in in all the slots. So I was wondering what the easiest way to store all the names in the arrays and print them out. The only thing i could find on the internet was to make an array of string arrays , but im not sure how to do that.

Thanks.

Look at it in a normal way. When you are assigning an array to hold a name as firstname[25] it can hold only one string of characters. So if you want to hold more names declare it as firstname[25][25]

With input taken as:

for(i=0;i<25;i++)
gets(char[i]);

When there is one single space for just a name and you go on writing many names in that space it will obviously print the last written name.

csurfer 422 Posting Pro

Hi C experts,
In my code I have got
#pragma extern_prefix (push, "");
Some extern function declarations
#pragma extern_prefix (pop, "");
So that compiler does not prepend anything to those extern functions , I get an error

error #14: extra text after expected end of preprocessing directive

Does anyone know how to fix this syntax error ?

I may be wrong but if this pragma is acting as preprocessor directive then it should not have any ";" at its end. Right?
(Even the error coming is also showing that...)

csurfer 422 Posting Pro

There are many flaws in your code as the multiple number of if's used and one need to change many lines of the program to get the required output.Try to write program such that number of stars can be varied without going through entire program. There are many ways to code it in a better way one of them already shown above by "Prabakar".
I will just stick to the concept of visualization I told earlier and write program as shown below:

#include<stdio.h>
#define MAXSTAR_LENGTH 9 //Number of stars you want(some odd number)

int main()
{
    int i=0,n,mid=MAXSTAR_LENGTH/2,lcount=0;

    while(lcount<MAXSTAR_LENGTH)
    {
	      for(n=0;n<MAXSTAR_LENGTH;n++)
	      {
			  if(n >= mid-i && n <= mid+i)
			  printf("*");
			  else
			  printf(" ");
	      }
	      printf("\n");
	      lcount++;
	      
              if(lcount<=mid)
	                      i++;
	      else
	                      i--;
    }

    return 0;
}

This is not the best program you can write for the above requisite but this one goes with my earlier idea of a space and filling lines with "*" or " ". So posting this.

csurfer 422 Posting Pro

thats a great idea but I m a beginner and I'm still not using arrays just loops.

That was an idea you can just use loops not arrays ok... Just loops
is enough.
Outer loop to count the rows and inner loop with some trick of yours within a if statement may be to print the *'s and " "s.

csurfer 422 Posting Pro

Hey all the beautiful help given to you above should increase the interest in you to build the program.The help given above is splendid read it again.

Well try this:

012345678
0123*5678
012***678
01*****78
0*******8
*********
0*******8
01*****78
012***678
0123*5678
012345678

If you assume the space to be a linear array from 0 to 8 as shown above which can be filled by * or " " then see the pattern you need to fill stars in and " " in and run your code. The above pattern might help you visualize it.

csurfer 422 Posting Pro

Well your requisites aren't clear so mention them in detail.

Apart from it you can initiate a char array and then get the input through simple scanf() function.

Then use strtok() with array address and space as delimiter to get the first token and then loop over and get the other tokens by using strtok() with NULL address and space as delimiter.

Both your requisites (or at least what i could make of them)are satisfied.

csurfer 422 Posting Pro

Well I am not able to point out your mistakes exactly but this code does work:

#include<stdio.h>

struct node{
int a;
char b;
};

typedef struct node * NODE;

int main()
{
NODE n;
int *in;
char *ch;
n=(NODE)malloc(sizeof(struct node*));
in=&(n->a);
ch=&(n->b);
*in=10;
*ch='a';
printf("%d %c",*in,*ch);
return 0;
}

I think the way in which you have defined the typedef of the struct is wrong.

Apart from that the initialization of pointer for struct or creating the sruct of some type as

<struct name> variable;

doesn't make the structure usable. You need to allocate memory for the structure using malloc as mentioned above.

csurfer 422 Posting Pro

A small addition to both the codes mentioned above.

The rule for being a Prime Number in maths states that a number is prime if it cannot be divided by any number (actually any prime number ) <= the square root of the number given.

that is:

for( i=2 ; i <= (int)sqrt(num) ; i++)

hence if you modify code as

#include<math.h>
//other includes

int main()
{
//your declarations

int sqr,prime=1;

//Accepting the number

sqr=(int)sqrt(num);

for(i=2;i<=sqr;i++)
{
                   if(num%i==0)
                   {
                                prime=0; 
                   }
}
if(prime)
                 printf("Prime number");
else
                 printf("Non prime");
return 0;
}

By applying the above mentioned rule you can reduce the number of comparison to nearly half and hence the efficiency increases highly in case of large numbers. Other mistakes have been rectified in post by "lucky chap".

csurfer 422 Posting Pro

csurfer: Do you always just repeat what other people have already said?

I am sorry if you felt so but when i started replying to this thread your post was not there and deleting a post is not in my hand.

csurfer 422 Posting Pro

Line no 79 84 even though not wrong are not needed...
Just lattice[j]++ or -- is enough.

line 21 and 24:

lattice = malloc (row * sizeof(int *));

lattice[i] = malloc(col * sizeof(int));

I may be wrong but i feel the memory allocation of single * and double * pointers are not done properly.
Here lattice is actually **lattice and lattice is *lattice and are different.

csurfer 422 Posting Pro

Well as much as I have seen your switch is not doing any thing other than going to the case n where n=lattice[j] and doing
table[n][1]=table[n][1]++

And with this thing in mind if the sole purpose of your switch is what I have stated above then your whole switch statement can be substituted with just one statement.

table[lattice[j]][1]++

csurfer 422 Posting Pro

malloc(<size>) just allocates a memory space for the size specified and returns a void * pointer pointing to the starting of the allocated memory block.
It returns a void * pointer (so that it can be type cast to any pointer type) and as you know void * pointer itself cannot be used to dereference any variable.So we need to typecast it according to our needs.
Example:

struct node{
int a ;
char b;
};
typedef struct node * NODE;

int main()
{
//Some code

NODE temp=(NODE)malloc(sizeof(struct node));

//Some code
}

General form:

<req>*<variable> = (<req>*)malloc(<size>);

csurfer 422 Posting Pro

malloc not freed and Array overflow errors as indicated by Aia should be checked and the error over flow in line no 64 causes error in every compilation and hence comes in notice.

But the same Array overflow error in Line 135 comes over only in certain cases and hence ought to be considered.

csurfer 422 Posting Pro

@ahamed101:

In my knowledge your usage of void * pointers are wrong.

The sole purpose of introduction of void * pointers in C is to include the concept of polymorphism and the basic limitation of void * pointers says that it cannot be dereferenced.

that is:

int *p,x=10;
void *v;

p=&x;    // Correct
printf("%d",*p);  //Correct
v=p;   //Correct

printf("%d",*v);  // Error as void* pointers cannot be dereferenced

Mostly they are used as intermediate elements in type casting of pointers.BUT CANNOT BE USED TO REFER TO VARIABLES DIRECTLY.

csurfer 422 Posting Pro

Excellent usage of the "LOCK" concept of OS in a very simple way in coding ANCIENT DRAGON. GREAT !!!

(Might be a bit difficult for starters to comprehend in the way it ought to be taken.)

csurfer 422 Posting Pro

Yes you need to remove the usage of "goto" statement.Below is one of the several ways how you can remove the goto.

repeat:

printf("\nEnter current year:");
scanf("%d",&cy);

printf("\nEnter current month:");
scanf("%d",&cm);

printf("\nEnter current date:");
scanf("%d",&cd);

printf("\nEnter birth year:");
scanf("%d",&by);

printf("\nEnter birth month:");
scanf("%d",&bm);

printf("\nEnter birth date:");
scanf("%d",&bd);

if (cy<=0 || cm<=0 || cd<=0 || by<=0 || bm<=0 || bd<=0)
{
               printf("\nEnter correct date");
               goto repeat;
}

You can change this as:

while(1)
{
             //Your code before the if.

             if (cy>0 && cm>0 && cd>0 && by>0 && bm>0 && bd>0)
            {
                    break;
            } 
            else
            {
                    clrscr();
                    printf("Enter correct date.")
            }
}

As you can see until you get a correct date you will be within while loop only so change your code like this its better coding style as all have said.

And your code doesn't have any problems on first look.

csurfer 422 Posting Pro

I may be wrong but what I feel is the pid_t data type within switch() is causing the error with -1. As negativity of the choice element is not correctly defined in some cases its better if you use an integer variable to hold the ForkPid value.

as
int pid;
.
.
pid=fork();
.
.
switch(pid)
{
.
.
}

This will satisfy your needs as the returned value of pid from a child process shall always come within the limits of signed integers.

csurfer 422 Posting Pro

18-2-21
was just an example the user can give any input..

"18-2-21" represents the format of input you need to take.You can generalize it as "<years>-<moths>-<days>" separated by just by "-" so that you can use "-" as the delimiter in the strtok( ,<delimiter>) function for your purpose. Other information are already in the earlier post.

csurfer 422 Posting Pro

And ya the ":" beside n indicates you need to assign that value to n
Ex:
It can be read as "Assign the length of array A to variable n" other statements also can be read in the same way...Its an algorithm.

csurfer 422 Posting Pro

The thing you have got here is an algorithm its not the actual code you need to look at it and code it on your own.

Here is a simple algorithm:

for i runs from 1 to ArrayLimit-1
{
    for j runs form 0 to ArrayLimit-i
    {
        compare Array[j] and Array[j+1]
        if Array[j] is greater  // Control Statement
        swap Array[j] and Array[j+1]
     }
}

I hope you can construct your code now.Above is for Sorting array in ascending order. Just by changing the control statement as

if Array[j+1] is greater

you can arrange it in descending order.

csurfer 422 Posting Pro

ok i need a little guide about my program.. How i take my start?
taking the current year,current month,current date,birth year, birth month, birth date input and giving the age in the form of "years-months-days" suppose "18 years-2months-21days"...

You can take "18-2-21" as a string input into string str and then use the
strtok(<str address>,"-") function to tokenise this and get it converted into integer by atoi() function to extract the year month and date values into integer variables.

Then just by using struct tm with clock() and using inbuilt int variables as tm_year,tm_mon,tm_mday you can continue.

csurfer 422 Posting Pro

Firstly...

for(b=0,b<=a,b--)
{
          printf("The Numbers in Descending order:%d\n",b[a]);
}

is wrong.As you can see you are starting b's count from 0 and doing a b-- operation.

Secondly...
You need some sorting mechanism to sort your arrray.The simplest to understand may be selection sort to start with.

csurfer 422 Posting Pro

I just said that loops are the best way to achieve the problem.

And if the person wants to continue in the format she has coded then she can use "goto" as mentioned above.

csurfer 422 Posting Pro

Loops are always better than goto but with goto I think this can be done:

You may also use another variable by the name cnt which would hold the number of times you want to re-run the program in case of a wrong input symbol and alter the code as:

cnt=<number of times you want to run loop>;
----program block----
.
.
.
else // The last else
cnt--;

if(cnt>0)
goto wrong;

----------------------------------------------
And your ' / ' operator will generate an error if num2 is 0 which is not taken care of in the code.