Turbo C/C++ and other junk compilers help, discussions and queries here

sarthak

In the zone
Re: Turbo C/C++ and other junk compilers discussions and queries here

k, will try it.
BTW which is the simplest C compiler.

But these are C++ compilers. Aint they different from C compilers?? I am looking for C compilers. And AFAIK C++ is different from C.
Am I wrong. If so plz do correct me. I am new to prog. And am totally unaware of all these.

There are two versions of C - C89 and C99. If you are new to programming you must be doing C89 in your college. C++ includes C89 but not C99. So you can compile C89 programs in those compilers mentioned above by simply saving them with extension ".c" instead of ".cpp". You need a C compiler only if you are going to do C99.
 

pratik385

In the zone
Re: Turbo C/C++ and other junk compilers discussions and queries here

MOD EDIT: Use this thread for junk compilers, use *www.thinkdigit.com/forum/programming/132924-c-c-beginners-guide-post-basic-questions-here.html for general C++ queries which doesn't involve the junk stuff. Likewise don't ask junk queries outside this thread. This post has been moved to the thread above - here.
 

nims11

BIOS Terminator
Re: Java Queries Here..

Wat s the difference b/w IDE and normal turbo c+ +
Is it just GUI n CUI

IDE - integrated development environment, which apart from being able to communicate with the compiler has features like editor, debugger, project management, etc.

TURBO C++ is an IDE with its compiler.

i think you mean to say the difference between turbo c++ and modern IDEs.
 

abhinav_sinha

Journeyman
Turbo c++ program problem!

code to be in turbo c++
Ok The question is :
WAP to generate a magic square of all possible combination (lowest no is 1 and all other integers less than equal to largest no. input by user). Data input by user: size of matrix, largest no in the matrix.
Magic square: The matrix is a square i.e. no. of rows=no. of columns.All the no are unique and principal diagonal sum=row sum=column sum=secondary diagonal sum.
ex:-
6 1 8
7 5 3
2 9 4
here: 6+1+8=6+5+4=8+5+2=6+7+2=1+5+9 and so on.(Hope question is clear enough.)

P.S.: I need solution in three days please help!
 

dbhaumik

Right off the assembly line
Re: Turbo c++ program problem!

void CalculateOddMagicSquare()
{
n=5;
int matrix[5][5];

int nsqr = n * n;
int i=0, j=n/2; // start position

for (int k=1; k<=nsqr; ++k)
{
matrix[j] = k;

i--;
j++;

if (k%n == 0)
{
i += 2;
--j;
}
else
{
if (j==n)
j -= n;
else if (i<0)
i += n;
}
}
}
 

abhinav_sinha

Journeyman
Re: Turbo c++ program problem!

void CalculateOddMagicSquare()
{
n=5;
int matrix[5][5];

int nsqr = n * n;
int i=0, j=n/2; // start position

for (int k=1; k<=nsqr; ++k)
{
matrix[j] = k;

i--;
j++;

if (k%n == 0)
{
i += 2;
--j;
}
else
{
if (j==n)
j -= n;
else if (i<0)
i += n;
}
}
}

Thanks for it but can you make it complete program for output to look like matrix (by complete I mean from #include to getch of main and of course it should ask the size of matrix and largest element just to set limitations on output if its not too much to ask.)
 

nbaztec

Master KOD3R
Why don't people do their own homework :/
dbhauvmik gave you a near complete program yet you won't do it yourself. I pity you. I seriously do.
 
can't find the bug

Can u please help me out?
consider the following code.

#include<stdio.h>
#include<conio.h>
main()
{
int num,p=0,n=0,z=0;
char choice='y';
while(choice=='y'||choice=='Y')
{
printf("Enter a number\n");
scanf("%d",&num);
if(num>0)
p++;
else if(num<0)
n++;
else
z++;

printf("Do you want to enter another number\n");
scanf("%c",&choice);
}

printf("positive number=%d negative=%d zero=%d",p,n,z);
getch();
}

when i run this code, after pressing any number, program goes out of the loop.

if i remove scanf("%c",&choice) and run the program, and press any alphabet key, it results in an infinite loop saying "do you want to enter another number",(WHY ?)

how can i fix the bug?
 
why am i getting an infinite loop?

//program to generate armstrong number less than 500
#include<stdio.h>
#include<conio.h>
main()
{
int num=1,i,a,b;
while(num<=500)
{
i=num;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
if((a*a*a)+(b*b*b)+(num*num*num)==i)
printf("\n%d",i);

num++;
}
getch();
}
 

nims11

BIOS Terminator
Re: can't find the bug

Can u please help me out?
consider the following code.

#include<stdio.h>
#include<conio.h>
main()
{
int num,p=0,n=0,z=0;
char choice='y';
while(choice=='y'||choice=='Y')
{
printf("Enter a number\n");
scanf("%d",&num);
if(num>0)
p++;
else if(num<0)
n++;
else
z++;

printf("Do you want to enter another number\n");
scanf("%c",&choice);
}

printf("positive number=%d negative=%d zero=%d",p,n,z);
getch();
}

when i run this code, after pressing any number, program goes out of the loop.

if i remove scanf("%c",&choice) and run the program, and press any alphabet key, it results in an infinite loop saying "do you want to enter another number",(WHY ?)

how can i fix the bug?

replace scanf("%d",&num); with scanf("%d\n",&num);
replace scanf("%c",&choice); with scanf("\n%c",&choice);
 
Last edited:

nims11

BIOS Terminator
Re: why am i getting an infinite loop?

//program to generate armstrong number less than 500
#include<stdio.h>
#include<conio.h>
main()
{
int num=1,i,a,b;
while(num<=500)
{
i=num;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
if((a*a*a)+(b*b*b)+(num*num*num)==i)
printf("\n%d",i);

num++;
}
getch();
}
the condition for the while loop to continue is num<=500. Inside the loop however, you are dividing the num by 10 and then incrementing it by 1. So you can imagine if the num will ever cross 500!
in your case, num is one. inside the while loop,
num=num/10
.
.
num=num/10
.
.
num++
so num always remains 1.

to solve this problem, take a separate variable, say num2 and assign the value of num to it in the beginning of the loop.
 

sakumar79

Technomancer
Re: why am i getting an infinite loop?

You are dividing num by 10 and then adding one to it in each loop. How will num increase?
num=num/10=1/10=0 (integer value), then num++ will make it num=1. It will forever be in loop since it will not change...

Arun

PS: A couple of minutes too late with my reply I see... Already answered.
 

Vyom

The Power of x480
Staff member
Admin
Re: can't find the bug

if i remove scanf("%c",&choice) and run the program, and press any alphabet key, it results in an infinite loop saying "do you want to enter another number",(WHY ?)

It's obvious that if you remove the scanf("%c",&choice); statement that you will stuck in an infinite loop, since the value of choice will always be y and condition will never be false.
And there's another way to take input to char. Try that. Keeping in mind the nature of the question, I hope LFC_fan won't mind the alternate statement that I am going to write.

@amrutansu garanaik : Replace that line with: choice = getch(); or choice = getche();
 

somulesnar

Journeyman
Re: C/C++ Beginner's Guide and Post Basic Questions here

Hey guys this is a simple question to find the gcd using euclidean algorithm...

Question: The process is based on the observation that, if r is the remainder when a
is divided by b, then the common divisors of a and b are the same as the
common divisors of b and r. Thus we can use the equation
gcd(a, b) = gcd(b, r)
to successively reduce the problem of computing a GCD to the problem of
computing the GCD of smaller and smaller pairs of integers. For example,
gcd(36,20) = gcd(20, 16) = gcd(16, 4) = gcd(4, 0) = 4
implies that the GCD of 36 and 20 is 4. It can be shown that for any two
starting numbers, this repeated reduction eventually produces a pair where the
second number is 0. Then the GCD is the other number in the pair.
Write a method called gcd that takes two integer parameters and that uses
Euclid's algorithm to compute and return the greatest common divisor of the
two numbers.



#include <stdio>

int euclid(int a,int b)
{
if(b==0)
return a;
else
return euclid(b,a%b);
}

int main()
{
int n1,n2;
cout<<"Enter two numbers to find its GCD:"<<endl;
cin>>n1>>n2;
cout<<"The GCD of n1 and n2 is"<<euclid(n1,n2)<<endl;
return 0;
}

This is a simple code that i hv written to solve this and hopefully it worked. But when i tried to put on a diffrent logic using recursion i am getting errors and the code fails to execute...
Here is the recursive code tht i have written. Need ur expertise suggestions on this as i am a novice to this simple yet strong object oriented programmig language c++...

#include<stdio.h>
class Euclid
{
static int gcd(int a, int b)
{
int r=a%b;
if(r==0)
{
return b;
}
else
{
int recurse=gcd(b,r);
return recurse;
}
}
void main()
{
int p;
int q;
cout<<"Enter the value of p"<<endl;
cin>>p;
cout<<"Enter the value of q"<<endl;
cin>>q;
cout<<gcd(p,q);
}
 

nims11

BIOS Terminator
Re: C/C++ Beginner's Guide and Post Basic Questions here

^^why are you going through the trouble of defining the function in the class?
Code:
int gcd(int a,int b)
{
    if ( b == 0 )
    return a;
    else
    return gcd(b,a%b);
}
 

nims11

BIOS Terminator
@nims11

yes i did tht to make the use of class concept in the program....but did tht create any prob in the code??

yes, your class concept code is full of all kinds of problems.
Here, i corrected your code
Code:
#include<iostream>
using namespace std;
class Euclid
{
    public:
    static int gcd(int a, int b)
    {
        int r=a%b;
        if(r==0)
        {
            return b;
        }
        else
        {
            int recurse=gcd(b,r);
            return recurse;
        }
    }
};
int main()
{
    int p;
    int q;
    cout<<"Enter the value of p"<<endl;
    cin>>p;
    cout<<"Enter the value of q"<<endl;
    cin>>q;
    cout<<Euclid::gcd(p,q);
    return 0;
}
 

somulesnar

Journeyman
@nbaztec

I didnt copy it actually i just had gone through the code that my frnd vickybat had posted...he gave me a wider scope to the euclidean concept....And if a person wants to learn then he is free to learn frm all scopes tht he wants or he gets and thts called learning or gaining knowledge frm wat he see's and not copying....plz consider ur suggestion...i agree tht am a novice to this thread and as well as to the language so plz try to help me not to demoralize me frm posting in the thread. Its not an advice but a simple request......

@nims11

Thnx for ur correction it helped me a lot..
 
Top Bottom