vinitmittal2008 37 Junior Poster

Ya, thanks buddy... i think you are confused with my name, its vinit mittal. Just call me vinit. :)

vinitmittal2008 37 Junior Poster

okk... i compiled it now... one more error is there .. use of getche().. use getchar() instead of getche() function.. both in write and append functions...

vinitmittal2008 37 Junior Poster

Hi vinitmittal,

Have u noticed one thing while reading the file.The read function prints only the last string that u entered. Because the file mode that you are using is wrong.

When you open a file, you must specify how it is to be opened. This means whether to create it from new, overwrite it and whether it's text or binary, read or write and if you want to append to it. This is done using one or more file mode specifiers which are single letters "r", "b", "w", "a" and + (in combination with the other letters). "r" - Opens the file for reading. This fails if the file does not exist or cannot be found. "w" - Opens the file as an empty file for writing. If the file exists, its contents are destroyed. "a" - Opens the file for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; this creates the file first if it doesn't exist. Adding + to the file mode creates three new modes:
"r+" Opens the file for both reading and writing. (The file must exist.) "w+" Opens the file as an empty file for both reading and writing. If the file exists, its contents are destroyed.
"a+" Opens the file for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after …

vinitmittal2008 37 Junior Poster

Hi vinitmitta,

The code is incomplete.

ya, thanks buddy .. i also noticed.. actually i posted complete code but i think there was some problem.. well here is the code

#include<conio.h> // avoid using this header file
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

int main() // don't use void with main function
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);



	//Actual Processing:
	do
	{
		//Print the Menu:
		printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
		printf("\nEnter '4' to exit");
		scanf("%d",&ch);
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}

	}while(ch!=4);
 return 0;
}

void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	if(fw==NULL)
	{
	 printf("\n Error: File Not Found!");
	 return;
	}
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	printf("\n\n File Successfully read!");
	fclose(fw);
}

void write(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"w");
	if(fw==NULL)
	{
	  printf("\n Error: Can't create new File!");
	  return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully written!");
	fclose(fw);
}

void append(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"a+");
	if(fw==NULL)
	{
	 printf("\n Error: Can't open File!");
	 return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully appended!");
	fclose(fw);
}
vinitmittal2008 37 Junior Poster

Try this one also...

#include<conio.h>   // try to avoid conio.h
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

int main() // don't use void with main
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);



	//Actual Processing:
	do
	{
		//Print the Menu:
		printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
		printf("\nEnter '4' to exit");
		scanf("%d",&ch);
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}

	}while(ch!=4);
  return 0;
}
void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	if(fw==NULL)
	{
	 printf("\n Error: File Not Found!");
	 return;
	}
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	printf("\n\n File Successfully read!");
	fclose(fw);
}
vinitmittal2008 37 Junior Poster

yes, that's the way

a=3;
b=4;
printf("%d/%d",a,b);
vinitmittal2008 37 Junior Poster

One little question bit off-toppic. I get an array of chars ended with a binary zero. I need to count how many chars do I have in the array. Can I check the ending like this?

while(s[i]!=0) { length++; i++}

I'm a bit confused about what a binary zero is. Do I get it right that it is equal to a regular zero? :)

EDIT: And how can I add the binary zero to the end of my own array? :D

Please start new thread for new question....

>>And how can I add the binary zero to the end of my own array?

I think you are talking about '\0' . A char string is always terminated by '\0' char.

vinitmittal2008 37 Junior Poster

May be thats the problem... well you are the better person who can understand your code...

vinitmittal2008 37 Junior Poster

well could'nt find any problem in your code... its happening maybe because of scanf function...

vinitmittal2008 37 Junior Poster

My code for this function is:

node * combine(node *l,node *m){
       node *p,*q;
       if(l==NULL)
	  p=m;
       else{
	  q=l;
	  p=q; // storing first node address in p
	  l=l->next;
	  while(m!=NULL){
	    q->next=m;
	    m=m->next;
	    q=q->next;
	    if(l==NULL){
	      q->next=m;
	      break;
	    }
	   else{
	      q->next=l;
	      q=q->next;
	      l=l->next;
	   }
	 }
       }
   return p;
  }

But I have to use Additional nodes p and q in this Function... What should i need to do for making it without additional nodes...

vinitmittal2008 37 Junior Poster

Write a 'C' Function to combine two singly connected linked lists in the following manner. Suppose one list is "L" which can be given by L=(l0,l1,.....,lm) and the other list is "M" where "M" can be expressed as M=(m0,m1,......,mn) where each li,mi represents one node in the respective lists. For simplicity, you may assume that each node contains an integer as the data. After combining them the combined list should be (l0,m0,l1,m1,.....,). Do not Use any additional node for writing the Function...

Please give some idea about Implementing this Function...!!

vinitmittal2008 37 Junior Poster

nice work dude!!

But could'nt get the logic.. :)

vinitmittal2008 37 Junior Poster

Google Array Examples in C

vinitmittal2008 37 Junior Poster

Before For loop initialize all elements of array hist_grade[6] to 0.

line 22: why are you destroying all values of grade to 0.. can't get the need of this line..
switch statement should me inside for loop..

line 24:

switch(grade[32])

should'nt it be

switch(grade[i])

line 28: how can you use it..

++hist_grade[i];

The values of i varies from 0 to 31 and hist_grade[6] can hold 0 to 5..
It should be

++hist_grade[0];

Line 33: ++hist_grade[1];

Line 38: ++hist_grade[2]; and so on...

vinitmittal2008 37 Junior Poster

Thanks Friends.. All suggestion are really helpful to me..

vinitmittal2008 37 Junior Poster

codeblocks-10.05-setup.exe 27 May 2010 23.3 MB BerliOS
codeblocks-10.05mingw-setup.exe 27 May 2010 74.0 MB BerliOS

NOTE: The codeblocks-10.05mingw-setup.exe file includes the GCC compiler and GDB debugger from MinGW.

which one should i need to download??

vinitmittal2008 37 Junior Poster

thanks... i will google it.

vinitmittal2008 37 Junior Poster

Please give Free download links of Latest and Best C/C++ Compilers for Windows...

vinitmittal2008 37 Junior Poster

Thanks for the suggestion... well now i tried this..

# include <stdio.h>

void show(int (*)[3],int,int);

int main()
{
  int a[3][3]={1,2,3,
	       4,5,6,
	       7,8,9};

  show(a,3,3);
  return(0);
}
void show(int (*p)[3],int row,int col)
{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",p[i][j]);
     printf("\n");
   }
}

Is it right way??

Snehamathur commented: Nice & Clear +0
vinitmittal2008 37 Junior Poster

I just want to learn how to pass a two dimensional Array to a Function using Pointer..

vinitmittal2008 37 Junior Poster

well I think you want to say this...

# include <stdio.h>
void show(int **,int,int);
int main()
{
  int a[3][3]={1,2,3,
	       4,5,6,
	       7,8,9};

  show(a,3,3);
  return(0);
}
void show(int **p,int row,int col)
{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",p[i][j]);
     printf("\n");
   }
}

But its showing two errors...

Cannot convert 'int[3] *' to 'int * *'

Type mismatch in parameter 1 in call to 'show(int * *,int,int)'

vinitmittal2008 37 Junior Poster

Hello Friends I want to pass a Double Dimension Array to a Function using Pointers...

Right Now i am doing this..

# include <stdio.h>
void show(int *,int,int);
int main()
{
  int a[3][3]={1,2,3,
	       4,5,6,
	       7,8,9};
  
  show(a[0],3,3);
  return(0);
}
void show(int *p,int row,int col)
{
  int i,j;
  printf("\n\n\n");
  for(i=0;i<row;i++)
   {
     for(j=0;j<col;j++)
	printf("\t %d",*(p+i*col+j));
     printf("\n");
   }
}

Is it correct way to pass a 2 D array?

vinitmittal2008 37 Junior Poster

Here you went Wrong !!

Are you sure that you will input 80 char long string every time...

while(i!=80)

It should be like

while(array[i]!='\0') // A string in c comes to end when it reaches '\0'

Also you are running a never ending while loop because there is no incrementation of i inside while loop..

i++;

Also this line is wrong

if(array[i]==(('a')||('A')||('e')))

Check for all vovels a,e,i,o,u and make it like

if ( array[i] == ('a') || array[i] == ('A') || array[i] == ('e') || array[i] == ('E'))

Also add condition for 'i' ,'o' ,'u' in the above if statement..

And print

printf("%d vowels, %d characters, %d others\n",vo,co,ot);
vinitmittal2008 37 Junior Poster

can you post your complete code here.... then it would be easier to understand exactly what's going on....

vinitmittal2008 37 Junior Poster

sir, what do you mean by specifying the path, could you give me an example
and i checked the folder it does not have my work saved in it...
thank you

Specifying the path means telling the compiler where you want your File on Disk...

like:
ofp = fopen("D:\\xyz\\myprogram.txt", "w"); // D is Drive Name & xyz is folder name on that drive....

vinitmittal2008 37 Junior Poster
char* my_strrev(char *p){
   int i,l,temp;
   for(l=0;*(p+l+1);l++);
   for(i=0;i<l;i++,l--){
     temp=p[i];
     p[i]=p[l];
     p[l]=temp;
   }
 return p;
}

This Function will work like strrev() Function...

vinitmittal2008 37 Junior Poster
/* program tht replaces two or more consecutive blanks in a string by a single blank.. */
# include<stdio.h>
int main(){
int i;
char s[50],*p,*q;
printf("\n\n :");
gets(s);
 for(i=0;s[i];i++){
  if(s[i]==32){
     p=&s[i+1];
    while(*p && *p==32){
	  q=p;
	  while(*q){
	  *q=*(q+1);
	  q++;
	  }
     }
  }
 }
printf("\n\n :%s",s);
return 0;
}
vinitmittal2008 37 Junior Poster

Here is My Circular Queue of Arrays Program.. and its working fine...

/* Implementation of a circular queue of Array containg names.. */
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# define QSIZE 5
typedef struct{
    int count;
    int head,tail;
    char names[QSIZE][30];
}QUEUE;
char* enqueue(char *);
char* dequeue();
void display();
void init();
QUEUE *pq;
int main(){
    int choice;
    char str[30];
    QUEUE q;
    pq=&q;
    init();
    do{
     clrscr();
     printf("\n\n\t\tEnter your Choice:");
     printf("\n\t\t:1 for Add into the Queue.. ");
     printf("\n\t\t:2 for Delete from the Queue.. ");
     printf("\n\t\t:3 Display Elements of the Queue.. ");
     printf("\n\t\t:4 For Exit..\n\t\t\t :: ");
     scanf("%d",&choice);
     switch(choice){
       case 1:
	       printf("\n\n\t\t Enter a Name:");
	       fflush(stdin);
	       gets(str);
	       puts(enqueue(str));
	       break;
       case 2:
	       puts(dequeue());
	       break;
       case 3:
	       display();
	       break;
       case 4: exit(0);
       default: printf("\n\n\t\t Plz press 1,2,3 or 4 key..");
     }
     printf("\n\n\n\t Press Any Key to continue...");
     fflush(stdin);
     while(!kbhit());
    }while(1);
    return 0;
}
void init(){
     pq->head = pq->tail = pq->count= 0;
}
char* enqueue(char *p){
     if(pq->count==QSIZE)
       return "\n\n\t\t Error: Queue Overflow..!!";
     pq->tail= (pq->tail)%QSIZE;
     strcpy(pq->names[(pq->tail)++],p);
     pq->count++;
     return "\n\n\t\t Element Successfully Inserted";
}
char* dequeue(){
     if(pq->count==0)
       return "\n\n\t\t Error: Queue Underflow..!!";
     pq->head= (pq->head)%QSIZE;
     pq->count--;
     printf("\n\n\t\t Deleted Queue Element is :");
     return pq->names[(pq->head)++];
}
void display(){
     int i=pq->head;
     int x=0;
     if(pq->count==0)
      printf("\n\n\t Queue is Empty..");
     else{
      while(x<pq->count){
	if(i==QSIZE)
	 i%=QSIZE;
	printf("\n\t\t :%s",pq->names[i]);
	i++;
	x++;
      }
    }
}
vinitmittal2008 37 Junior Poster

In the circular queue you may not rely on relative head and tail positions. You must keep track of the number of elements manually. Extend the struct queue with an int count initialized to 0. Increment it on every enqueue and decrement on every dequeue operations. Rewrite is_empty and is_full to inspect the count and act accordingly.

Of course, upon reaching the end of array, head and tail shall go back to 0.

PS: I'd recommend to test the queue state inside enqueue and dequeue.

Thanks Buddy.... Your suggestion was really helpful to me...

vinitmittal2008 37 Junior Poster

Sorry to say but that is not a linked list -- its just an array. Linked lists look somethig like this:

typedef struct _tagNode
{
   struct _tagNode* next;
   struct _tagNode* prev; // circular list
   char name[30]; // node data
} NODE;

NODE* head = 0;
NODE* tail = 0;

This program is implementetion of a queue of array containing names.. i want to make a circular Queue of array.

vinitmittal2008 37 Junior Poster

Hello Friends...
i made this Queue Program in c and its working good.. But I want to make it circular Queue please give some ideas..

# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# define QSIZE 5
typedef struct{
    int head,tail;
    char names[QSIZE][30];
}QUEUE;
void enqueue(char *);
char* dequeue();
void display();
int is_full();
int is_empty();
void init();
QUEUE *pq;
int main(){
    int choice;
    char str[30];
    QUEUE q;
    pq=&q;
    init();
    do{
     clrscr();
     printf("\n\n\t\tEnter your Choice:");
     printf("\n\t\t:1 for Add into the Queue.. ");
     printf("\n\t\t:2 for Delete from the Queue.. ");
     printf("\n\t\t:3 Display Elements of the Queue.. ");
     printf("\n\t\t:4 For Exit..\n\t\t\t :: ");
     scanf("%d",&choice);
     switch(choice){
       case 1: if(is_full())
		printf("\n\n\t\t Error: Queue Overflow..!!");
	       else{
		 printf("\n\n\t\t Enter a Name:");
		 fflush(stdin);
		 gets(str);
		 enqueue(str);
	       }
	       break;
       case 2: if(is_empty()){
		printf("\n\n\t\t Error: Queue UnderFlow..!!");
		init();
	       }
	       else
		printf("\n\n\t\t Deleted Queue Element is %s",dequeue());
	       break;
       case 3: if(is_empty())
		printf("\n\n\t\t Error: Queue is Empty..!!");
	       else
		display();
	       break;
       case 4: exit(0);
       default: printf("\n\n\t\t Plz press 1,2,3 or 4 key..");
     }
     while(!kbhit());
    }while(1);
    return 0;
}
void init(){
     pq->head = pq->tail = 0;
}
int is_full(){
    return pq->tail == QSIZE;
}
int is_empty(){
    if(pq->head == pq->tail){
     init();
     return 1;
    }
    else
     return 0;
}
void enqueue(char *p){
     strcpy(pq->names[(pq->tail)++],p);
     printf("\n\n\t\t Element Successfully Inserted");
}
char* dequeue(){
     return pq->names[(pq->head)++];
}
void display(){
     int i;
     for(i=pq->head;i<pq->tail;i++)
       printf("\n\t\t :%s",pq->names[i]);
}
vinitmittal2008 37 Junior Poster

line 2: delete conio.h because its non-standard and non-portable.

line 16: void main(){

It's always int main() void main() is non-standard and therefore non-portable.

well... thanks for your suggestions..
i am using conio.h for clrscr() function... what else should i use instead of clrscr()..

vinitmittal2008 37 Junior Poster

Hello Friends..
I need suggestions to make my this program better...

Implementation of a STACK(containing names) as an array, with its basic operations PUSH,POP & SHOW..

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <process.h>
# define STACKSIZE 5
typedef struct{
   int size;
   char names[STACKSIZE][20];
  }STACK;
void push(char *);
char *pop();
void show();
int is_empty();
int is_full();
STACK *ps;
void main(){
 int choice;
 char name[20];
 STACK s;
 ps=&s;
 ps->size=0;
 do{
   clrscr();
   printf("\n\t  :1 for Push. ");
   printf("\n\t  :2 for Pop. ");
   printf("\n\t  :3 for Show.");
   printf("\n\t  :4 for Exit.");
   printf("\n\t Enter your choice:");
   scanf("%d",&choice);
   switch(choice){
    case 1:
      if(is_full())
	printf("\n\t Error: Stack Overflow..");
      else{
	printf("\n\n\t Enter a Name:");
	fflush(stdin);
	gets(name);
	push(name);
      }
      break;
    case 2:
      if(is_empty())
	printf("\n\n\t Error: Stack Underflow..");
      else{
	printf("\n\n\t Poped up Name is:");
	puts(pop());
      }
      break;
    case 3:
      if(is_empty())
	printf("\n\n\t The Stack is Empty..");
      else{
	printf("\n\n\t Current Stack is:\n\t\t");
	show();
      }
      break;
    case 4:
      exit(0);
    default:
      printf("\n\n\t Press only 1,2,3,4 keys..");
   }
   while(!kbhit());
  }while(1);
}

int is_empty(){
    return ps->size==0;
  }
int is_full(){
    return ps->size==STACKSIZE;
  }
void push(char *pc){
     strcpy(ps->names[(ps->size)++],pc);
     printf("\n\n\t  Successfully Pushed into STACK");
  }
char * pop(){
     return ps->names[--(ps->size)];
  }
void show(){
     int x=ps->size;
     while(x!=0)
       printf("\n\t\t %s",ps->names[--x]);
  }
vinitmittal2008 37 Junior Poster

to make a char array
of 2 columns containing strings 49 characters long and
10 rows contaning strings also 49 characters long.

is this correct char ArrayName[10][50][2][50] ??

what i get is you want a char array that have 10 rows and each row contains a 49 char string...
then it could be done by simply
char charname [10][50]

vinitmittal2008 37 Junior Poster
#include<stdio.h>
int main()
{
    int i,j,k,c;   
    char s[1000];  //array to store the input string
    for(i=0;(c=getchar())!='.';++i)  //string to be ended with '.'
    {
        s[i]=c;                        
        for(j=0;j<=i;++j)               
        {
            if((s[j]==s[j-1])&&(s[j]=='\n' || s[j]=='\t' ||s[j]==' '))
            {
                j=j-1;
            }
        }
        for(k=0;k<=i;++k)
        printf("%c", s[k]);
    }
    return 0;
}

I am trying to create a program that will remove a consecutive occurrence of white space character. But the output is not as intended. Pls tell me where i went wrong ???? A little support will be helpful !!

may be u could get some help from my this program...

/* program tht replaces two or more consecutive blanks in a string by a single blank.. */
# include<stdio.h>
# include<conio.h>
void main(){
int i;
char s[50],*p,*q;
clrscr();
printf("\n\n :");
gets(s);
 for(i=0;s[i];i++){
  if(s[i]==32){
     p=&s[i+1];
    while(*p && *p==32){
	  q=p;
	  while(*q){
	  *q=*(q+1);
	  q++;
	  }
     }
  }
 }
printf("\n\n :%s",s);
getch();
}