xavier666 56 Junior Poster

I just made a 'Hello World' program

#include<stdio.h>
int main()
{
	printf("Hello World\n");
    return 0;
}
exe file size    = 15.5 KB
Source file size = 79.0  B

Then i made a data structure program (implementing stack, queue, deque)

exe file size    = 21.9 KB
Source file size = 17.3 KB

So i guess it's not linearly dependent with its source file
So what really decides the size of an exe file.
PS - I think it is also compiler dependent 'coz exe files on turbo c++ are larger compared to those of code blocks

xavier666 56 Junior Poster

O.K., try a 'quasi' recursion solution

LOL

uskok, what you're doing is just traversing with the help of recursion. But there is no doubt that the job will get done. But the actual problem is the summation of the problem.
For example, the first code is an example of true recursion

int true_recursion_factorial(int n)
{  
    if(n == 1)
        return 1;
    else if(n == 0)
        return 1;
    else
        return n * true_recursion_factorial(n - 1);
}

It treats memory as stack
This one is false recursion

answer = 1;     // global variable
int false_recursion_factorial(int n)
{
    if(n >= 1)
    {
        answer *= n;
        n -- ;
        false_recursion_factorial(n);
    }
    else if (n == 0)
        answer = 1;
    
    return  answer;
}

This one simply stores the answer in a place.
I can't explain the exact difference actually in writing to satisfy you. But i'm sure both of them are not equal.

xavier666 56 Junior Poster

Strictly speaking, this problem is not recursive in nature.
What you can do is to let the function call itself (while traversing through the array) and with the help of some conditional operators check when the predefined sum is equal to the running sum inside the function.
But this is not recursion. This is just a function calling itself

xavier666 56 Junior Poster

Even after starting C, most books (or links) only say what we should do. But the user(for me example) remains ignorant about the "DO NOTS", and by mistake, might indulge into the wrong programming practices. This link will surely help.

xavier666 56 Junior Poster

I've tried to make a DEQUE program. My book on data structures (Seymour Lipschutz) doesn't have an algorithm on DEQUES. Neither does DADS give a good description.
All I heard was that it could be implemented better with two lists (one forward and one backward) & there are 2 types of DEQUES (INPUT & OUTPUT restricted). But the program has become too long. So I was wondering if the code can be made shorter.
Please offer your (constructive) criticisms

#include <stdio.h>
#include <malloc.h>

/*
 *===========================================================
 * PROGRAM DESCRIPTION - IMPLEMENTATION OF A DEQUE          |
 * AUTHOR              - SUMITRO BHAUMIK AKA "XAVIER666"    |
 * DATE                - 11TH DECEMBER, 2009                |
 * COMPILER            - CODE::BLOCKS 8.02                  |
 * OS                  - WINDOWS XP SP2                     |
 *===========================================================
 *
 * NOTES - I THINK THE RESTRICTION CODES (RC) NEEDS TO BE EXPLAINED
 * SINCE DEQUES CAN BE EITHER INPUT OR OUTPUT RESTRICTED,
 * ALL I DID WAS JUST NOT LET THE FUNCTION BE ACCESSSED.
 * THE RCs ARE CLOSELY LINKED WITH THESE (LINE 120) SWITCH CASE VALUES
 * IF THE USER WANTS INPUT TOP RESTRICTED, RC = 1
 * DURING THE DEQUE OPERATIONS MENU, IF THE RC IS SAME AS SWITCH CASE VALUE,
 * THAT OPTION IS RESTRICTED. THE OTHER RC CODES OPERATE THE SAME WAY
 */

typedef struct deque
{
	int data;
	struct deque *forward;
	struct deque *backward;
}node;

void push_top( int );       // SAME AS PUSHING IN STACK
void push_end( int );       // SAME AS PUSHING …
xavier666 56 Junior Poster

but you should put forth roughly the same detail and/or effort as the poster did in asking the question

Apt

Thanks

xavier666 56 Junior Poster

Hello bgspace,
I apologize for troubling you to convert the comments and outputs to English. If I can't understand a language, it means I have to improve myself or let someone else handle the situation(my bad jephthah).
It seems that you still have not changed your compiler. (I recommend code::blocks)
Before posting your new code, you should have read jephthah's post
Please attach any custom header file that you may have used in your code
Line 25 - Don't use fflush(stdin)

xavier666 56 Junior Poster

I was expecting someone to explain him the problems about his code. Ah well ...

Hi harshchandra,
Thank you for presenting your code snippet but your code has some major faults, hence lost it's appeal. Your program displays the bad practices of C. They are as follows

  • conio.h - It is not a standard C header file and is only used in old MS-DOS compilers. You can see about it here
  • void main() - Let's just say 'void main'ers are doomed' (-Salem)
  • goto - There are a lot of alternatives to goto . It is against structured programming
  • getch() & clrscr() come under conio.h, so no need to repeat

This is an excellent link about the bad practices of C (explaining WHY they are bad) and what to do to avoid them.

anonymous alias commented: pay attention, dude. this crap is over 5 years old. it doenst need obvious commentary. +0
xavier666 56 Junior Poster

Ya, it's worked. Thanks

xavier666 56 Junior Poster

Okay, one last thing Narue, when i'm using the function, what parameter will I send the the function?
I was thinking stdin but I maybe wrong but I wanted to be sure(It worked though!)
Will it be something like this ->

printf("\n\n\tPress Enter To Continue ... ");
getchar();
interactive_flush(stdin);
xavier666 56 Junior Poster

Don't listen to them endofworld201, I'll help you cheat. Here is my code, follow this and you'll be way ahead of your class before you know it

#include<stdio.h>
int main()
{
	printf("\nRead the forum rules");
	printf("\nRead your C text book");
	return 0;	
}
xavier666 56 Junior Poster

All this looks very exciting but you have got to understand that some of us don't understand Bulgarian (Had to Google it and I'm still not sure). Please convert the program outputs & comments in English.
You should have read the forum rules which clearly states to write "in full-sentence English".

jephthah commented: if you can't understand C, then you need to quit "helping" -1
xavier666 56 Junior Poster

but she didn't taught us how to

She didn't teach you because it's for you to figure out (of course I'm assuming your teacher has taught you the basics)

You missed to read the rules of the forum. We don't give any code away without you posting what you have done to show effort.

I was wondering whether we can even post our algorithms if the thread starter didn't even post a single code or algorithm of his/her own.

xavier666 56 Junior Poster

A BIG bow to DAVE
A bow to Narue is becoming redundant these days

xavier666 56 Junior Poster

but you probably won't get into that for a while

Actually I will be getting into it 'coz my next semester in college will be totally about data structures. So i was thinking if i could finish the syllabus before the holidays ...
Thanks for DADS

xavier666 56 Junior Poster

It can be done using strings though. But I've not thought about it yet. The nature of this problem is similar to adding two 50 digit decimal numbers which has to done by strings

xavier666 56 Junior Poster

Ya, mine is a one-liner but it failed the last point on my list. So I was wondering whether there was any other alternative ...
Thanks for the "function" though ...

xavier666 56 Junior Poster

During run-time of a menu driven program, I want the user to see what option the user has entered. Something like this :-

[OUTPUT]
.
.
.
Enter Option : [1] <- [I]User given[/I]

Press Any Key To Continue ... [_] <- [I]Blinking Cursor[/I]
.
.
.
[/OUTPUT]

After I hit enter, the program will continue as usual.

My requirements

  • The code should be compiler independent
  • Should be a one-liner
  • Should not utilize a separate variable

The list is according to my priority. If No. 1 fails, No. 2 should not fail. If No. 2 fails, No. 3 should not fail. If No. 3 fails, then i have nothing more to say

I once tried something like this,

int n;
printf("Enter Any Number & Then Press Enter To Continue ... ");
scanf("%d", &n);

But this looks ugly and I had to use a separate variable.
So, in a nutshell, I'm looking for an alternate of the non-standard
getch();

xavier666 56 Junior Poster

Also, why are you posting C code in a C++ forum..??!

As far as i know, all C code can be compiled as C++ code. But as per forum instructions, it should be moved.

xavier666 56 Junior Poster

By the way, that link didn't have anything about stacks, queues, circular queues, dequeues, priority queues.
I should have mentioned it though, my bad but my priority is with the above mentioned list

xavier666 56 Junior Poster

If you're insulted by people correcting your mistakes, you need to adjust your attitude. Why? Because unless you're some kind of C demigod, you're going to make mistakes and people will correct them

With all due respect, in my locality, nobody gives a damn about using conio.h . It's only here that I learned the problems associated with conio.h . So all that I'm asking is for a little patience. Compared to everyone else here, I'm just a hatchling

clrscr and getch are just as non-portable in C++

Then I think it maybe due to the fact that our teachers gave Turbo C++ from the beginning

I'll be learning as I go

xavier666 56 Junior Poster

>The problem is that you have not flushed stdin. Put
>fflush(stdin); before the scanf . That should do it.
Yes, that should do it. "It" being break the program by invoking undefined behavior. Please don't recommend poor practices.

>Try using gets(str1); and gets(str2); in place of scanf()
Seriously dude, you should learn a little more about C before trying to help people. You're pushing the worst possible options. Have you never learned that gets is 100% unsafe and impossible to make safe?

I'm still learning. All of my teachers never reacted to my code like the way you people did but I guess that's because of their .. uh .. "inexperience".
No seriously, Narue, point noted about fflush(stdin)

Jeez have guys heard of "the good samaritan act", I'm sure the person thought he/she was helping. Did it require three senior posters calling this person down...

I'm really grateful for supporting me. I posted with all the best intentions

>Jeez have guys heard of "the good samaritan act", I'm
>sure the person thought he/she was helping.
He was helping, and I'm sure he can be a valuable asset to the forum. But not correcting the mistakes of good Samaritans would be grievous negligence on our part.

>Did it require three senior posters calling this person down...
Whatever it takes to get the point across.

Narue, I never need to be told twice about a mistake. One "scolding" would be enough.
And …

xavier666 56 Junior Poster

keyzzz_it, please provide the complete program, along with the header files.
It seem that you have provided a very sketchy program. Give the complete program within code tags. It's beneficial both ways

xavier666 56 Junior Poster

First of all sorry for sending unorganised code, am new to this website

Mistakes happen, no big deal.

I am working in Fedora core 6 OS

Maybe that's the problem because I'm using XP. It's working perfectly fine in my computer.
Try using gets(str1); and gets(str2); in place of scanf() What compiler are you using?

xavier666 56 Junior Poster

The errors were :-

  • Writing %C instead of %c
  • Placing the brackets incorrectly

The corrected code is as follows:-

#include <stdio.h>
#include <stdlib.h>
//GIGO.c- Coded by Ross Camarillo aka Bubonic_88
// on 12/02/2009
//Created by Randy Gibson
#define SYMB '*' // character to show in the letters



void letter_G (char SM);
void letter_I (char SM);
void letter_O (char SM);
void displayline (void);

int main (void)
{
	letter_G(SYMB);
	displayline();
	letter_I(SYMB);
	displayline();
	letter_G(SYMB);
	displayline();
	letter_O(SYMB);
	getchar(); // to cause system pause
	return(0);
}

// child functions

void displayline (void) // display the line break in the main function
{
	printf("-----\n");
}
void letter_G (char SM)
 //display ther letter g
{
	printf(" %c%c%c \n",SM,SM,SM);
	printf("%c   \n",SM);
	printf("%c %c%c%c\n",SM,SM,SM,SM);
	printf("%c   %c\n",SM,SM);
	printf(" %c%c%c \n",SM,SM,SM);
}
void letter_I (char SM)
 // display the letter i
{
	printf("%c%c%c%c%c\n",SM,SM,SM,SM,SM);
	printf("  %c  \n",SM);
	printf("  %c  \n",SM);
	printf("  %c \n",SM);
	printf("%c%c%c%c%c\n",SM,SM,SM,SM,SM);
}
void letter_O(char SM)
{
	printf(" %c%c%c \n",SM,SM,SM);
	printf("%c   %c\n",SM,SM);
	printf("%c   %c\n",SM,SM);
	printf("%c   %c\n",SM,SM);
	printf(" %c%c%c \n",SM,SM,SM);
}

A better program would be writing it in a single line

xavier666 56 Junior Poster

Here is the make-shift algorithm

  • Take the number to be worked upon from user. Call it NUM
  • Try to find a number from 1 to half of the NUM which divides NUM and leaves no remainder
  • If you find such a number, check whether the number is prime or not
  • If it is prime, print the number

Algorithm for prime

  • Take the number to be worked upon from user. Call it PRM
  • From 2 to half of PRM, take numbers and divide PRM with them.
  • If, on division, there is no remainder, signal that PRM is not prime
  • If, at the end of all such divisions, no divisor has been found, signal that PRM is prime

Try to convert this into code

xavier666 56 Junior Poster

Points to be noted, raghuhr84

  • Give the complete program, that is, give #include<stdio.h> . Becomes easier for us to check.
  • Use proper indentation
  • Use code tags around your program (See the forum rules)

The problem is that you have not flushed stdin. Put fflush(stdin); before the scanf . That should do it.
Here is the complete program :-

#include <stdio.h>

int main()
{
	char str1[100];
	char str2[100];

	printf("Enter 1st string\n");
	fflush(stdin);
	scanf("%[^\n]s",&str1);

	printf("Enter 2nd string\n");
	fflush(stdin);
	scanf("%[^\n]s",&str2);

	printf("1st string is %s\n", str1);
	printf("2nd string is %s\n", str2);

	return 0;
}
kvprajapati commented: Well said. +6
Aia commented: fflush(stdin) invokes undefined behaviour -1
jephthah commented: flushing an input buffer is meaningless, and adatapost should know better. -1
ssharish2005 commented: Read the manual and be clear beforr you post. fflush is not meant to flush stdin it for stdout. +0
iamthwee commented: Well said +11
xavier666 56 Junior Poster

First of all meghs, your pattern is not properly indented, it should be

*
    *   *
  *   *   *
*   *   *   *
  *   *   *
    *   *
      *

Second, you should have shown us how much you have tried. Let your code be full of errors, we'll point out your errors. But don't expect fully fledged answers for you to copy-paste. It is against the spirit of learning

Third, lostfate, you shouldn't have given out the answer just like that. Help him/her to understand the nature of the problem. At least someones brain neurons would have been tickled.

Forth, lostfate, please follow the standard coding practices, that is, int main() (with return ) and not just main()

xavier666 56 Junior Poster

You are a bit lazy

I am
And a (precise) link about data structures ...

xavier666 56 Junior Poster

If your aim is to do the thing in one line, you can use ?:

salesTrend = (soldYesterday > soldToday) ? -1 : 0
xavier666 56 Junior Poster

That post has become to big ;)
I wish you could have give me a more precise link
Ah well ...

xavier666 56 Junior Poster

Listen fedya, if what Narue said is correct (that you have to INVENT a new algorithm for sorting) you have to keep two things in mind

  • It should be faster that quick sort
    OR
  • It should be highly efficient for a particular type of data arrangement

Scientists are spending sleepless nights trying to find newer methods of sorting. If you are a scientist (which I doubt, no offense bub), you should get in touch with some of your MIT buddies and have a good chat with them. Only a few selected individuals can help you here

If you are NOT trying to invent an algorithm, then try to rephrase the question because I seriously can't understand what you are trying to say.

By the way, I just remembered I made a sorting algorithm but I can't say whether it's any good or not. Just check whether it's different from BUBBLE and SELECTION

PS - Please don't give -ve rep for "giving away the answer", I'm just showing what I've produced.

#include<stdio.h>

int main()
{
	int a[10];	int i;
	int j;    	int tmp;
	int m;

	//INPUT
	printf("\n\n\tEnter The Numbers\n");
	for(i = 0;i < 10;i++)
	{
		printf("\t");
		scanf("%d", &a[i]);
	}

	//DISPLAY OF ORIGINAL ARRAY
	printf("\n\n\tThis Is Your Original Array : \n\n");
	for(i = 0;i < 10;i++)
		printf("%d\t", a[i]);

	printf("\n\n____________________________________________\n\n");

	for(i = 0;i < 9;i++)
	{
		for (j = i + 1;j < 10;j++)
		{
			if (a[i] > a[j])
			{
				tmp  = a[i];
				a[i] = …
xavier666 56 Junior Poster

Hello everybody, I just started learning C and decided to join this site. But as soon as I posted some of my code snippets here, immediately I got a stream of insults (thanks to conio.h ). But it's not my fault, I've grown on clrscr() and getch() (due to my experience with C++).

I'm looking for a solution here. Can anyone give me a link to such a compiler which does not support conio.h . I've read the thread "Starting C" and downloaded DEV C++ but it also supported conio.h and hence I wasted my precious MBs. It should be light weight and not be resource hungry.

Second is, I want a good book(pdf would be nice) about C DATA STRUCTURES. Even a site will do.

xavier666 56 Junior Poster

Thanks Dave :$

xavier666 56 Junior Poster

Is there a seperate function provided by C to sort and swap?

Sort values in an array without using sort functions (Urgent)

What does this mean anyway?
Are you asking to make a custom sorting function?

xavier666 56 Junior Poster

here's a bug: #include <conio.h> your program is worthless with that in it. why do you people insist on clinging onto that Turbo C crap?

I'm actually new. Can you please explain the problems faced with conio.h I've been getting insults just for using this.
PS - Why did they (don't know who) put conio.h in C in the first place if it was such a problem?

why do you people insist on clinging onto that Turbo C crap?

What should i be clinging to?

xavier666 56 Junior Poster

This type of problem is best solved with the help of recursion
Here is the algorithm
accept [number]
if number is greater than 9
extract numbers and multiply them together till the original [number] becomes zero
send the result as parameter to this function
if number is not greater than 9
return the answer

As simple as that. I assure you this algorithm is 100% working

xavier666 56 Junior Poster

The program generates a calendar for one year. The program only asks for the day on which 1st January falls from user.
Notes :

  • Compiler - Turbo C++ 4.5
  • Leap year has been considered
  • The only reason for including conio.h is because of clrscr() . If somebody can give me a one-line alternative, I would be happy to modify the code
  • The user can either view a particular month or the whole year
  • Error handling has not been implemented (feeling lazy :yawn:)

Please don't flame me for using global variables. I don't know about the problems of global variables so enlighten me :P
PM me if you find any bugs or if you think it can be improved

jephthah commented: c code snippets are supposed to be useful. anything compiled in TurboC is useless for most of western civilization. if you wont stop using it, then at least stop subjecting others to it. -1
xavier666 56 Junior Poster

Please for Christ's sake give the complete program

xavier666 56 Junior Poster

You can also do this

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int hiya;
    cout<<"hello world \n";
    getch();
    cout<<"enter a number \n";
    cin>>hiya;
    cout<<"you entered \n"<< hiya;
    getch();
}
xavier666 56 Junior Poster

It looks some issue in deleting the entry..

Fixed

xavier666 56 Junior Poster

Okay, this is a temporary fix. Love it or leave it :p

#include <stdio.h>
#include <alloc.h>
#include <conio.h>

typedef struct student
{
	int roll;
	int age;
	struct student *link;
}std;

void append(int,int);			//	Creating the database for the 1st time
void display(void);				//	Displays the entire database
void search(int);              	//	Searches for a particular roll number
void delRoll(int);              //	Deletes an entry according to the given roll number
void addAtBegin(int,int);   	//	Inserts an entry in the begining
void addAtLoc(int,int,int);    	//	Inserts an entry at the given location
void addAftRoll(int,int,int);   //	Inserts an entry after a specified roll number
void noOfStudents(void);		//  Finds the total number of students

std *head;	std *temp;	std *temp2;	std *add;

int main()
{
	int n;	int r;	int a;	int pos;	int fRoll;

	do
	{
		clrscr();
		printf("\n\t================");
		printf("\n\tSTUDENT DATABASE");
		printf("\n\t================\n");
		printf("\n\t1 - Apending Information About A Student");
		printf("\n\t2 - Displaying Information Of All Students");
		printf("\n\t3 - Searching Information About A Student");
		printf("\n\t4 - Deleting Information About A Student According To Roll No");
		printf("\n\t5 - Adding Information About A Student At Begining");
		printf("\n\t6 - Adding Information About A Student At Any Location");
		printf("\n\t7 - Adding Information About A Student After A Roll Number");
		printf("\n\t8 - Finds Total Number Of Students");
		printf("\n\t9 - Exit");
		printf("\n\n\tChoice : ");
		fflush(stdin);
		scanf("%d",&n);
		switch(n)
		{
			case 1 :
				clrscr();
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				append(r,a);
				getch();
				break;
			case 2 :
				clrscr();
				printf("\n\n\tInformation List\n\n");
				display();
				getch();
				break;
			case 3 :
				clrscr();
				printf("\n\n\tEnter The Roll Number …
xavier666 56 Junior Poster

why did my answer get a negative point? :(

xavier666 56 Junior Poster

remove conio.h

I wanted to do that but I need a C equivalent of clrscr()
Otherwise, the output looks bad :'(

delete all those global variables

I've seen that, in my compiler, if i declare a variable globally, the problem of garbage values disappears. If only there was a constructor in C, this problem would have never risen :sad:

a few comments would be nice to explain what those functions do

I thought the names were enough ... but okay, I'll give the comments but will you improve my grades? :P

I'll edit the code later but i will ...

xavier666 56 Junior Poster

There you go

#include <iostream.h>
#include <conio.h>

int i;
int a[10];

int main()
{
	cout<<"\n\n\tEnter The 10 Numbers : \n";
	for(i=0;i<10;i++)
	{
		cout<<"\t";
		cin>>a[i];
	}

	cout<<"\n\n\tThe 10 Numbers In Reverse : \n\t";
	for(i=9;i>=0;i--)
	{
		cout<<a[i]<<"\t";
	}
    return 0;
}
mrnutty commented: Don't give out the answer just yet. +0
xavier666 56 Junior Poster

This is just a linked list program for those who need help understanding the fundamentals. Put comments if you find some bugs.
The program is about maintaining a student database (their roll number and their age)
I've used Turbo C++ 4.5 as the compiler

:icon_cool:

xavier666 56 Junior Poster
#include <stdio.h>

int i;	int j;	int row;	int column;
int sum[10][10];
int sub[10][10];

void dimension(void)
{
	printf("\n\n\tEnter The Dimensions Of The Matrix : ");
	printf("\n\n\t\tROW    :  ");
	scanf("%d", &row);
	printf("\n\n\t\tCOLUMN :  ");
	scanf("%d", &column);
}

void display(int *m[][10],int r,int c)
{
	for(i = 0;i < r;i++)
	{
		printf("\n\n\t");
		for(j = 0;j < c;j++)
		{
			printf("%d\t",(*(*(m + i) + j)));
		}
	}
}

void input(int *m[][10],int r,int c)
{
	printf("\n\n\tEnter The Elements : \n\n");
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			printf("\t");
			scanf("%d",&(*(*(m + i) + j)));
		}
	}
}

void addition(int m1[][10],int m2[][10],int r ,int c)
{
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			sum[i][j] = m1[i][j] + m2[i][j];
			printf("[%d][%d]\n",m1[i][j],m2[i][j]);
		}
	}
}

void subtraction(int m1[][10],int m2[][10],int r ,int c)
{
	for(i = 0;i < r;i++)
	{
		for(j = 0;j < c;j++)
		{
			sub[i][j] = m1[i][j] - m2[i][j];
		}
	}
}

int mat1[10][10];
int mat2[10][10];

int main()
{
	dimension();
	input(mat1,row,column);
	input(mat2,row,column);

	printf("\n\n\tMATRIX 1 : ");
	display(mat1,row,column);
	printf("\n\n\tMATRIX 2 : ");
	display(mat2,row,column);


	addition(mat1,mat2,row,column);
	subtraction(mat1,mat2,row,column);

	printf("\n\n\tRESULT OF ADDITION    : ");
	display(sum,row,column);
	printf("\n\n\tRESULT OF SUBTRACTION : ");
	display(sub,row,column);

	return 0;
}

Tell me wats wrong in my code. Urgent!!