944,025 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 359
  • C++ RSS
Nov 3rd, 2009
0

Explanation of code line by line

Expand Post »
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;

}
Similar Threads
Reputation Points: 8
Solved Threads: 0
Newbie Poster
abraham james is offline Offline
3 posts
since Nov 2009
Nov 3rd, 2009
2
Re: Explanation of code line by line
The blank lines do nothing.

Start with them.
Reputation Points: 40
Solved Threads: 2
Light Poster
rdrast is offline Offline
30 posts
since Apr 2008
Nov 3rd, 2009
0
Re: Explanation of code line by line
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.
Last edited by William Hemsworth; Nov 3rd, 2009 at 12:53 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Nov 3rd, 2009
0
Re: Explanation of code line by line
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:

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h> // Include "stdio.h" (C library to perform Input/Output operations)
  2. #include <stdlib.h> // Include "stdlib.h" (C Standard General Utilities Library)
  3.  
  4. #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
  5. #define pi 3.14 // Same as above, just with 3.14 everytime "pi" occurs.
  6.  
  7. void pusha(int i); // Function Prototype
  8. int pop(void); // Function Prototype
  9. void sorta(int stacka[SIZE],int numbers); // Function Prototype
  10. int *pointer, stacka[SIZE],numbers; // Create a pointer, an array and a var
  11.  
  12. int main(void)
  13. {
  14. int value,num,num2,num3,num4,array[5],num6,num7,num8; // Make a lot of vars
  15. float num5,num9; // And a couple of floats
  16. num=0; // Set the var num=0
  17. pointer = stacka; // Make the global pointer point to the global stack
  18.  
  19. printf("************************************************\n"); // Output to screen
  20. printf("----------------PROJECT-------------------------"); // Output to screen
  21.  
  22. printf("\nYOU SHOULD INPUT INTEGERS;\nTHEIR TOTAL NUMBER SHOULD BE DIVISIBLE BY THREE\n"); // Output to screen
  23. printf("\nenter the number of integers you want to input::"); // Output to screen
  24. scanf("%d",&numbers); // Scan for input, and write the input to the variable "numbers"
  25. 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.
  26. {
  27. printf("the number is not divisible by 3\n");
  28. printf("TRY AGAIN;\nenter the number again::");
  29. scanf("%d",&numbers);
  30. }
  31.  
  32. do { // Start a Do-While loop
  33. printf("\nEnter the values consequitively: "); // Output to screen
  34. scanf("%d", &value); // Scan for input, and write the input to the variable "value"
  35.  
  36. if(value != 0) // If value is different from zero, then run the following:
  37. {
  38. pusha(value); // Call the function Pusha and send it the var "value".
  39. }
  40.  
  41. num++; // Indent the variable "num".
  42.  
  43. }
  44. while(num<=(numbers-1)); // Run the Do-While loop, while num is less or equal to (numbers-1)
  45.  
  46. printf("\n\n\nelements in the stack are::"); // Output to screen
  47. for(num=0;num<=(numbers-1);num++) // Run for loop for output, to output numbers
  48. {
  49. printf(" %d",stacka[num]); // Output to screen
  50. }
  51. printf("\n\nlets now sort it\n"); // Output to screen
  52. printf("\npress 1 to sort it::"); // Output to screen
  53. scanf ("%d",&num3); // Scan for input, and write the input to the variable "num3"
  54. if(num3==1) // If num3 equals 1, then run the following, if it dosn't exit the program.
  55. {
  56. sorta(stacka,numbers);
  57. }
  58. else
  59. {
  60. return 0; // Exit the program
  61. }
  62.  
  63.  
  64. for(num6=0;num6<=((numbers/3)-1);num6++)
  65. {printf("\n\nwe pop the elements in 3's\n");
  66. for(num=0;num<=2;num++)
  67. { array[num]= pop();
  68. printf(" %d",array[num]);
  69. }
  70.  
  71.  
  72. num2= array[0]*array[1];
  73. num7=2*(array[0]+array[1]);
  74.  
  75. if(array[0]==array[1])
  76. {
  77. printf("\n\nthe first 2 elements form sides of a square whose area is %d",num2);
  78. printf("\nthe perimeter of this square is %d::",num7);
  79. }
  80. else
  81. {printf("\n\nthe first 2 elements form sides of a rectangle whose area is %d",num2);
  82. printf("\nthe perimeter of this rectangle is %d::",num7);
  83.  
  84. }
  85. num4=array[2]*array[2];
  86. num5=num4*pi;
  87. num8=2*array[2];
  88. num9=num8*pi;
  89. printf("\n\n\nthe the 3rd element forms the radius of a circle whose area is %f",num5);
  90. printf("\nthe perimeter of this circle is %f::\n",num9);
  91.  
  92. }
  93. printf("\n\n\n****************END OF PROGRAM*******************\n\n");
  94. }
  95.  
  96.  
  97. void pusha(int i) // The pusha Function
  98. {
  99. *pointer=i; // Write "i" (value) to the pointer
  100. pointer++; // Indent the pointer
  101. }
  102.  
  103. void sort(int stacka[SIZE],int numbers)
  104. {
  105. int num,t,number;
  106. for (number=0;number<=(numbers-1);number++)
  107. for(num=0;num<=(numbers-1);num++)
  108. {
  109. if (stacka[num]>stacka[num+1])
  110. { t=stacka[num];
  111. stacka[num]=stacka[num+1];
  112. stacka[num+1]=t;
  113. }}
  114. printf("\nthe sorted array is::");
  115. for(num=1;num<=numbers;num++)
  116. {
  117. printf(" %d",stacka[num]);
  118. }}
  119.  
  120. int pop()
  121. {int m;
  122. m=*pointer;
  123. pointer--;
  124. return m;
  125. }
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Skeen is offline Offline
77 posts
since Nov 2009
Nov 3rd, 2009
0
Re: Explanation of code line by line
Quote ...
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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Merging 2 Files
Next Thread in C++ Forum Timeline: hash table /independent rehaching





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC