May somebody pliz help explain to me line by line of this c++ coding cause I can't seem to understand it:-

#include <stdio.h>
#include <stdlib.h>

#define SIZE 50
#define pi 3.14
void push(int i);
int pop(void);
void sort(int stack[SIZE],int numbers);
int  *pointer, stack[SIZE],numbers;

void main(void)
{
  int value,numbers,num,num2,num3,num4,array[5],num6,num7,num8;
float num5,num9;
  num=0;
 pointer = stack; // pointer points to the  stack 

printf("************************************************\n");
printf("----------------PROJECT-------------------------");

 printf("\nYOU SHOULD INPUT INTEGERS;\nTHEIR TOTAL NUMBER SHOULD BE DIVISIBLE BY THREE\n");
printf("\nenter the number of integers you want to input::");
scanf("%d",&numbers);
while((numbers%3)!=0 ||numbers==0 )

{printf("the number is not divisible by 3\n");
  printf("TRY AGAIN;\nenter the number again::");
  scanf("%d",&numbers);}

  do {
    printf("\nEnter the values consequitively: ");
    scanf("%d", &value);

    if(value != 0) 
    {push(value);
    }

    num++;
  } while(num<=(numbers-1));
printf("\n\n\nelements in the stack are::");
  for(num=0;num<=(numbers-1);num++)
{
printf(" %d",stack[num]);

}
printf("\n\nlets now sort  it\n");
printf("\npress 1 to sort it::");
scanf ("%d",&num3);
if(num3==1)
{sort(stack,numbers);
}
else
{
exit(1);
}




for(num6=0;num6<=((numbers/3)-1);num6++)
{printf("\n\nwe pop the elements in 3's\n");
    for(num=0;num<=2;num++)
{   array[num]= pop();
    printf("  %d",array[num]);
}


num2= array[0]*array[1];
num7=2*(array[0]+array[1]);

if(array[0]==array[1])
{
printf("\n\nthe first 2 elements form sides of a square whose area is %d",num2);
printf("\nthe perimeter of this square  is %d::",num7);
}
else
{printf("\n\nthe first 2 elements form sides of a rectangle whose area is %d",num2);
printf("\nthe perimeter of this rectangle is %d::",num7);

}
num4=array[2]*array[2];
num5=num4*pi;
num8=2*array[2];
num9=num8*pi;
printf("\n\n\nthe the 3rd element forms the radius of a circle whose area is %f",num5);
printf("\nthe perimeter of this circle is %f::\n",num9);

}
printf("\n\n\n****************END OF PROGRAM*******************\n\n");
}


void push(int i)

 {*pointer=i;
 pointer++;}

void sort(int stack[SIZE],int numbers)
{
int num,t,number;
for (number=0;number<=(numbers-1);number++)
for(num=0;num<=(numbers-1);num++)
{
    if (stack[num]>stack[num+1])
    { t=stack[num];
        stack[num]=stack[num+1];
       stack[num+1]=t;
}}
printf("\nthe sorted array is::");
for(num=1;num<=numbers;num++)
{
    printf("  %d",stack[num]);
}}

int pop()
{int m;
m=*pointer;
pointer--;
return m;

}

Recommended Answers

All 4 Replies

The blank lines do nothing.

Start with them.

No, nobody is going to explain that badly formatted C code to you which should have been wrapped in code-tags. You clearly didn't write it, so what I suggest you do is learn how to program, buy a book, whatever works.

First of all, I don't think that; "Void Main()" is ISO standard, I think you should stick to "Int Main()".

Secondly you're redeclaring numbers in Main, after making it a global variable.

Thirdly you're calling; "push(value);", which isn't really calling your own function, it's calling "push(const value_type& __x)" inside the "stl_queue.h" libary, atleast that's what my compiler does.

Quadly? the function "sort(stack,numbers);" is being called inside the "stl_algo.h" on my compiler

And Heres how far I got in the code b4 i gave up:

#include <stdio.h>  // Include "stdio.h"  (C library to perform Input/Output operations)
#include <stdlib.h> // Include "stdlib.h" (C Standard General Utilities Library)

#define SIZE 50     // Define SIZE to 50, basically seaches the file for the word SIZE and replaces it with 50, this is done before the projekt is being compiled, simply by the precompiler
#define pi 3.14     // Same as above, just with 3.14 everytime "pi" occurs.

void pusha(int i);                       // Function Prototype
int pop(void);                          // Function Prototype
void sorta(int stacka[SIZE],int numbers); // Function Prototype
int *pointer, stacka[SIZE],numbers;      // Create a pointer, an array and a var

int main(void)
    {
    int value,num,num2,num3,num4,array[5],num6,num7,num8; // Make a lot of vars
    float num5,num9;                                      // And a couple of floats
    num=0;                                                // Set the var num=0
    pointer = stacka;                                      // Make the global pointer point to the global stack

    printf("************************************************\n");    // Output to screen
    printf("----------------PROJECT-------------------------");      // Output to screen

    printf("\nYOU SHOULD INPUT INTEGERS;\nTHEIR TOTAL NUMBER SHOULD BE DIVISIBLE BY THREE\n");     // Output to screen
    printf("\nenter the number of integers you want to input::");                                  // Output to screen
    scanf("%d",&numbers);                  // Scan for input, and write the input to the variable "numbers"
    while((numbers%3)!=0 ||numbers==0 )    // While "numbers" isn't zero, and "numbers" is dividable with 3, then run the next lines. Untill the entered number is correct.
        {
        printf("the number is not divisible by 3\n");
        printf("TRY AGAIN;\nenter the number again::");
        scanf("%d",&numbers);
        }

    do { // Start a Do-While loop
    printf("\nEnter the values consequitively: ");  // Output to screen
    scanf("%d", &value); // Scan for input, and write the input to the variable "value"

    if(value != 0)       // If value is different from zero, then run the following:
        {
        pusha(value);    // Call the function Pusha and send it the var "value".
        }

        num++;           // Indent the variable "num".
        
        }
    while(num<=(numbers-1));  // Run the Do-While loop, while num is less or equal to (numbers-1)
    
    printf("\n\n\nelements in the stack are::");      // Output to screen
    for(num=0;num<=(numbers-1);num++)   // Run for loop for output, to output numbers
    {
    printf(" %d",stacka[num]);           // Output to screen
    }
    printf("\n\nlets now sort it\n");       // Output to screen
    printf("\npress 1 to sort it::");       // Output to screen
    scanf ("%d",&num3);                     // Scan for input, and write the input to the variable "num3"
    if(num3==1)                             // If num3 equals 1, then run the following, if it dosn't exit the program.
        {
        sorta(stacka,numbers);
        }
    else
        {
        return 0;  // Exit the program
        }


for(num6=0;num6<=((numbers/3)-1);num6++)
{printf("\n\nwe pop the elements in 3's\n");
for(num=0;num<=2;num++)
{ array[num]= pop();
printf(" %d",array[num]);
}


num2= array[0]*array[1];
num7=2*(array[0]+array[1]);

if(array[0]==array[1])
{
printf("\n\nthe first 2 elements form sides of a square whose area is %d",num2);
printf("\nthe perimeter of this square is %d::",num7);
}
else
{printf("\n\nthe first 2 elements form sides of a rectangle whose area is %d",num2);
printf("\nthe perimeter of this rectangle is %d::",num7);

}
num4=array[2]*array[2];
num5=num4*pi;
num8=2*array[2];
num9=num8*pi;
printf("\n\n\nthe the 3rd element forms the radius of a circle whose area is %f",num5);
printf("\nthe perimeter of this circle is %f::\n",num9);

}
printf("\n\n\n****************END OF PROGRAM*******************\n\n");
}


void pusha(int i) // The pusha Function
     {
     *pointer=i;  // Write "i" (value) to the pointer
     pointer++;   // Indent the pointer
     }

void sort(int stacka[SIZE],int numbers)
{
int num,t,number;
for (number=0;number<=(numbers-1);number++)
for(num=0;num<=(numbers-1);num++)
{
if (stacka[num]>stacka[num+1])
{ t=stacka[num];
stacka[num]=stacka[num+1];
stacka[num+1]=t;
}}
printf("\nthe sorted array is::");
for(num=1;num<=numbers;num++)
{
printf(" %d",stacka[num]);
}}

int pop()
{int m;
m=*pointer;
pointer--;
return m;
}

First of all, I don't think that; "Void Main()" is ISO standard, I think you should stick to "Int Main()".

Secondly you're redeclaring numbers in Main, after making it a global variable.

Thirdly you're calling; "push(value);", which isn't really calling your own function, it's calling "push(const value_type& __x)" inside the "stl_queue.h" libary, atleast that's what my compiler does.

He didn't write the code, nor does he understand it, so he's asking for someone to baby him through each line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.