How is the stack and stack pointer work?
A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Stack pointer is the pointer that points to the top of the stack or that points the item at the top of the stack and help in adding or deleting the item from the top of stack.
There are a few different ways to do this but this one will work just fine for your problem. (note these numbers are not truly random, but they will probably be random enough for your purposes)
you will have to:
import java.util.Random;
then define a new random number generator called Generator:
Random generator = new Random(); Then, make a random integer between 0 and 40 and subtract 20 so it is between -20 and positive 20. int myNumber = generator.nextInt(40) - 20;
Can you please tell me the c program to print the following pattern... 1 123 12345 1234567?
This was checked for C++ using VS2008.
#include
using std::cin;
using std::cout;
using std::endl;
int main()
{
cout << endl << "Enter of digits in the last number of the patter "
<< endl << "(should be odd number, if you enter even number N," << endl << "the answer will have N - 1 digits in the last number): ";
int numElem = 0;
cin >> numElem;
cout << endl << "Pattern:" << endl;
for (int i = 1; i <= numElem; i += 2)
{
for (int j = 1; j <= i; j++)
{
cout << j;
}
cout << endl;
}
system("PAUSE");
return 0;
}
For just C you might want to use something like (not tested):
#include
int main()
{
cout << endl << "Enter of digits in the last number of the patter "
<< endl << "(should be odd number, if you enter even number N,"
<< endl << "the answer will have N - 1 digits in the last number): ";
int numElem = 0;
cin >> numElem;
cout << endl << "Pattern:" << endl;
for (int i = 1; i <= numElem; i += 2)
{
for (int j = 1; j <= i; j++)
{
cout << j;
}
cout << endl;
}
system("PAUSE");
return 0;
}
Explain in detail the basic organization of computer with neat diagram?
The basic organisation of computer includes, 1.Control Unit
2.Central Processing Unit
a)Control Unit
b)Arithmetic and Logical Unit
c)Memory Unit
*Primary Storage
*Secondary Storage
3.Output Unit
Calculate determinants of a nxn matrix in C programming?
from:
http://local.wasp.uwa.edu.au/~pbourke/other/determinant/
/*
Recursive definition of determinate using expansion by minors.
*/
double Determinant(double **a,int n)
{
int i,j,j1,j2;
double det = 0;
double **m = NULL;
if (n < 1) { /* Error */
} else if (n j1)
continue;
m[i-1][j2] = a[i][j];
j2++;
}
}
det += pow(-1.0,1.0+j1+1.0) * a[0][j1] * Determinant(m,n-1);
for (i=0;i<n-1;i++)
free(m[i]);
free(m);
}
}
return(det);
}
//New Answer By Shaikh SOHIAL Hussain form PAKISTAN
#include<stdio.h>
#include<conio.h>
void main ()
{ clrscr();
int a[10][10],row,i=0,j=0,result,w,x,y,z;
printf("This Program made 4 solve matrix determintae\n");
scanf("%d",&row);
while(i<row)
{printf("\n");
for(j=0;j<row;j++)
{ printf("Enter the value of %d%d\n",i,j);
scanf("%d",&a[i][j]);
}
i++;
}
i=0;
while(i<row)
{ printf("\n");
j=0; while( j<row)
{printf("%d",a[i][j]);
printf(" ");printf(" ");
j++;}
i++;
}
if(row==2)
{
result=(a[0][0]*a[1][1])-(a[0][1]*a[1][0]);
printf("The answer is\t %d",result);
}
else if(row==3)
{ result=(a[0][0]*((a[1][1]*a[2][2])-(a[1][2]*a[2][1]))) - (a[0][1]*((a[1][0]*a[2][2])-(a[1][2]*a[2][0]))) + (a[0][2]*((a[1][0]*a[2][1])-(a[1][1]*a[2][0])));
printf("\nThe answer is \t %d",result);
}
else if(row==4)
{
w=a[0][0]*(a[1][1]*(a[2][2]*a[3][3]-a[2][3]*a[3][2])-a[1][2]*(a[2][1]*a[3][3]-a[2][3]*a[3][1])+a[1][3]*(a[2][1]*a[3][2]-a[2][2]*a[3][1]));
x=a[0][1]*(a[1][0]*(a[2][2]*a[3][3]-a[2][3]*a[3][2])-a[1][2]*(a[2][0]*a[3][3]-a[2][3]*a[3][0])+a[1][3]*(a[2][0]*a[3][2]-a[2][2]*a[3][0]));
y=a[0][2]*(a[1][0]*(a[2][1]*a[3][3]-a[2][3]*a[3][1])-a[1][1]*(a[2][0]*a[3][3]-a[2][3]*a[3][0])+a[1][3]*(a[2][0]*a[3][1]-a[2][1]*a[3][0]));
z=a[0][3]*(a[1][0]*(a[2][1]*a[3][2]-a[2][2]*a[3][1])-a[1][1]*(a[2][0]*a[3][2]-a[2][2]*a[3][0])+a[1][2]*(a[2][0]*a[3][1]-a[2][1]*a[3][0]));
result=w-x+y-z;
printf("\n The answer is \t %d", result);
}
else{ printf("Determinate Limit is 4*4 max\n");
getch();
}
Program to find employee details using structure in c?
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[100];
int age,salary;
};
void main()
{
struct employee details;
clrscr();
printf("\n the name of the employee is ... \n the age of the employee is... \n the salary acquired is...");
scanf("%c%d%d",&details.name,&details.age,&details.salary);
clrscr();
printf("\n the name is... \n the age is... \n the salary is...",details.name,details.age,details.salary);
getch();
}
How do you function help to reduce the size of program in C?
Functions reduce code size in one of three ways, depending on the complexity of the function.
Normally, a function call results in the compiler generating code to call and return from the function, but the code for the function itself is only generated once. This not only reduces duplication in your source code and thus makes your code much easier to read and maintain, it also reduces the overall size of the machine code if the code required to implement the call and return mechanism results in fewer operations than the function itself. However, the call and return mechanism has a runtime cost, reducing performance slightly.
Some functions can be inline expanded (by the compiler) to the extent that the expansion results in smaller code than the normal call and return mechanism would incur. By eliminating the function call mechanism, we not only improve runtime performance, we can also reduce code size.
Inline expansion does not guarantee smaller code size; in some cases we may increase the size of the code. Although increased code size can have an affect on overall performance, this has to be balanced against the performance that is gained by eliminating the function call mechanism.
Finally, some functions can take advantage of compile-time computation, which is similar to inline expansion but where complex expressions can be evaluated at compile time and thus reduced to much simpler expressions. This not only reduces code size but greatly improves performance by completely eliminating unnecessary runtime operations.
What is mean by scope in c language?
Scope is generally defined as variable or functions life... Generally the life is between the opening braces and close braces "{ }" ... If thee variable is defined as "static" and if is defined outside a function then its scope is in same file. if defined as global then its scope is across the files.
What is the difference between a readline statement and a writeline statement?
The readline statement method of programming will allow for the next statement in the sequence to be read. The writeline method of programming only allows for the current statement or sequence to be read after determining the end of the last line.
What is passes in sorting algorithm?
Assuming you're talking about comparison-based sorting algorithms, the number of passes is the number of comparisons that the algorithm makes internally while sorting. In a programming language, this would be the total number of times the loop executes. This number is defined by the computational complexity (Big-O notation), which defines an upper bound.
Difference between heap sort and quick sort?
Bucket sort is a sorting algorithm that works by partitioning an array into a finite number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. A variation of this method called the single buffered count sort is faster than the quick sort and takes about the same time to run on any set of data.
How does a photovoltaic array work?
A photovoltaic array converts sunlight into electricity using solar cells made of semiconductor materials, typically silicon. When light photons strike the cells, they excite electrons, generating a flow of electric current through the material. This direct current (DC) electricity is then converted to alternating current (AC) by an inverter, making it usable for homes and businesses. The efficiency of the array depends on factors like the angle of sunlight, temperature, and the quality of the solar cells.
How do you print a string using putchar?
You would iterate over all characters within the string, printing each character with the putchar function. In C, strings are terminated with a null byte, so you'd stop when that null byte has been reached.
Example:
void printme(const char* me) {
while (*me) {
putchar(*me++);
}
}
Needless to say this method is inefficient compared to using API that outputs the entire string at once, but the general approach of iterating over all characters in a string is used frequently.
Convert the following binary and decimal number 396 into respective number system?
2 396 R
2 198 0
2 99 0
2 49 1
2 24 1
2 12 0
2 6 0
2 3 0
2 1 1
0 1
C program to read an array and print its reverse using pointers?
#include<stdio.h>
#include<conio.h>
// note: this code does not test for user-input errors!!
int main() {
int array[10],i,n;
printf("enter the number of elements in the array:");
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&array[i]);
printf("the reverse ordered array is :");
for(i=n-1;i>=0;i--) printf("%d",array[i]);
return 0;
}
Why java takes 4 byte of int while c has 2?
Java defined int as a 32-bit number because that is generally large enough to hold the information you need.
The size of an int in C may actually have either 16 or 32 bits, depending on the implementation. Basically, the specifications for any C implementation in UNIX must have 32-bit ints, while the ISO C standard only requires 16-bit ints. The stdint.h and limits.h files exist exactly because not all implementations are the same, and these files will define the min/max values of the integral types.
Do compiled programs usually run faster because they are already in machine code?
No it is because compiled programs are scared so they run like stink.
Plus, uncompiled programs, ie. source programs, do not run at all... neither slowly nor fast.
What are the different ways of constructing a multiway if-else structure?
If this is a one-off, use either SSI components from your junk-box, or one or more MUX chips, with some outputs wired back to inputs.
If you are making quantities, either use an eprom, burn it to ROM when it's debugged, or use a PLA.
Whichever you do, logical analysis first will probably save time and money in the long term.
What is singleton class it's implementation?
"A class containing a static variable that stores a unique, and inaccesible
to external classes (private), intance of itself. The static variable is
accessed by a static method, with public access, usually called getIntance.
The static variable is initiated by the static getInstance method that
validates wether or not the static variable already exits. If the static
variable has not being initiated, a new instance of the class is created
and assigned to the static variable which reference is then returned by the
method. If the static variable was previously created, the method will
return a reference to the static variable." 1
A Singleton class is used when you wish to restrict instantiation of a class to only one object.
"Simple Singleton Pattern Example in AS3
class Data
{
private static var dataInstance:Data;
public static function getInstance():Data
{
if(!dataInstance) dataInstance = new Data();
return dataInstance;
}
public function Data()
{
if(dataInstance) throw Error("instance exists, please use Data.getInstance()");
}
}
" 2
1 [Daniel Guzman - AS3 Object Oriented Programming]
2 [Daniel Guzman - AS3 Object Oriented Programming]
Structure is like a architects drawings for a building it is the basis and a plan we need for building anything solid upon,foundation and good plans for the structure show the way we wish things to be,these plans helps us if there are any blips along the way. Structure provides a plan and an idea for advancing an idea and letting it exist for as long as is necessary until other elements interfere with or change our original plans.
How do you write a program to find out largest number between two numbers using ternary operators?
// assume you want the find the largest of ints a,b,c:
int max = (a>b&&a>c?a:b>c?b:c);
C program for Sum of n number using if else statements?
// Why do you need if/else statements?
int main()
{
int numbers[] = {1, 2, 3, 4, 5, 6}; // and so on
int i;
int sum = 0;
for(i = 0; i < sizeof (numbers)/sizeof(int); i++)
sum += i;
return sum;
}
Where does global variables stored in C?
It depends entirely on what platform you are using.
In an embedded environment, for instance
global/static variables go into different RAM memory segments depending on whether or not they are initialised.
constants are often left in ROM
automatic variables are normally placed of the stack of the currently running task but not always.
Differences between Quicksort and selection sort?
Bubble sort is easy to program, slower, iterative. Compares neighboring numbers swaps it if required and continues this procedure until there are no more swaps
Quick Sort is little difficult to program, Fastest, Recursive. Pivot number is selected, other numbers are compared with it and shifted to the right of number or left depending upon criteria again this method is applied to the left and right list generated to the pivot point number. Select pivot point among that list.
/* For a short string, like "abaz" a Hashmap like (a:2, b:1, z:1) will be shorter, than a whole alphabet*/
#include<stdio.h>
#include<conio.h>
main()
{
int count,i,j;
char str[50];
printf("Enter string : ");
gets(str);
for(i=0;i<=strlen(str)-1;i++)
{
count=1;
if(str[i]==' ')
continue;
for(j=i+1;j<=strlen(str)-1;j++)
{
if(str[i]==str[j])
{
str[j]=' ';
count++;
}
}
printf("%c : %d \n",str[i],count);
}
getch();
}
/*Answered by Ankush Monga
Doing DOEACC B level*/