Why is using arrays with loops improve program efficiency?
Arrays provide the most compact method of representing one or more element of the same type in computer memory (whether in working memory or on disk). There is absolutely no memory overhead with arrays, other than when the array is allocated on the heap in which case you need to maintain at least one reference or pointer to the allocated memory. The structure of the array is built-in to the array itself, such that one element immediately follows another, and each element is exactly the same length (in bytes). This makes it possible to access any element in the array in constant time using simple pointer arithmetic. That is, knowing the start address of the array allows you to reference any element in the array using a memory offset or suffix operator. The first element is always found at offset zero (the start of the array) while the next is at offset 1. Thus for an n-element array, the final element will be at offset n-1. By offset, we really mean offset * sizeof (type), where type is the type of each element in the array. However, when working with arrays, the language knows the size of each type therefore we just use the offset as a zero-based index, where element 5 will be found at index 4, which is 4 * sizeof (type) bytes from the start of the array.
Given that arrays allow constant-time random access, loops make it extremely easy to traverse arrays from any element to any other element, both forwards and backwards. The loop control variable simply acts as the index to the element we wish to process on each iteration of the loop. We can also choose to skip elements if we're only interested in every other element, or every third element, simply by incrementing the control variable accordingly.
Looping through array indices is only efficient when you actually intend to traverse the array, such as when printing the entire array. When searching arrays for a specific value, loop traversal is the least efficient method. If we plan to search an array many times for many different values, it pays to sort the array first. We can then use the binary search technique, starting from the middle element. If that's not our element, the fact the array is sorted means we can eliminate one half of the array, depending on how the middle value compares to the value we are searching for. We then repeat the process with the remaining half, reducing the remaining elements by half each time until we either find our value, or the remaining half has no elements (in which case the value does not exist).
There are many ways to pronounce it. The most common are pronounced like kwark and kwork
Dennis M. Ritchie developed the C language from 1969 through 1973 while working at Bell Labs. Although Brian Kernighan is often attributed as co-developer, he was actually co-author of 'The C Programming Language' book which came out in 1978 and served as the informal specification for what became known as "K&R C". Ritchie's original version is now referred to as "Classic C". K&R C was later replaced by ANSI C and is covered by Kernighan and Ritchie's second edition of the same book.
How do you learn the c plus plus language?
The same way you understand any computer language -- by learning the language. However, the learning curve is steep so it helps if you learn a much simpler language first, such as BASIC, so that you are familiar with basic concepts such as variables, arrays, loops and so on.
Where you can include a header file in the program?
You can include a file with the #include directive at any place you want to. You just have to consider that the compiler will see the total source file as if you had copied the contents of each include file at the point where you included it, and it will parse and process the total source file accordingly.
That said, header files, a subset of included files, are generally #include'd at the top of the source file. Again, it all depends on what is in the include file.
Find the smallest number and its position using array with c coding?
You simply walk through the array and whenever you find a smaller element you remember its value and location.
E.g. If you have an array of integers called pArray that contains 100 elements, here's what the code might look like:
int cPos 0, int nMin pArray[0]; // Provisionally assume the first element is smallest.
for ( int n 1; n < 100; ++n )
{
if ( pArray[n] < nMin )
{
// We've found a new smaller element, record its position and value.
nMin pArray[n];
cPos n;
}
}
Program to implement push operation in stack using c language?
#include
#include
void push(int st[],int data,int &top);
void disp(int st[],int &top);
int pop(int st[],int &top);
int flg=0;
int top=-1,tos=-1;
int st[50];
void push(int st[],int data,int &top)
{
if(top==50-1)
flg=0;
else
{
flg=1;
top++;
st[top]=data;
}
}
int pop(int st[],int &top)
{
int pe;
if(top==-1)
{
pe=0;
flg=0;
}
else
{
flg=1;
pe=st[top];
top--;
}
return(pe);
}
void disp(int st[],int &top)
{
int i;
if(top==-1)
{
printf("\nStack is Empty");
}
else
{
for(i=top;i>=0;i--)
printf("\t%d",st[i]);
}
}
void main()
{
int dt,opt;
int q=0;
clrscr();
printf("This Program Is Used to Perform PUSH & POP operations On Stack");
printf("\n\n\tMain Menu.........");
printf("\n\n1.Push");
printf("\n\n2.Pop");
printf("\n\n3.Exit");
do
{
printf("\n\n\tEnter Your Choice 1-3:");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the Element to be Push:");
scanf("%d",&dt);
push(st,dt,tos);
if(flg==1)
{
printf("\nAfter Inserting the Element, Stack is:\n\n");
disp(st,tos);
if(tos==50-1)
printf("\nStack is Now Full");
}
else
printf("\nStack Overflow Insertion Not Possible");
break;
case 2:
dt=pop(st,tos);
if(flg==1)
{
printf("\n\tData Deleted From the Stack is:%d\n",dt);
printf("\n\tAfter Deleting the Element from the stack is:\n\n");
disp(st,tos);
}
else
printf("\nStack Empty,Deletio Not Possible:");
break;
case 3:
q=1;
break;
default:printf("\nWrong Choice Enter 1-3 Only");
}
}while(q!=1);
}
OUTPUT
Main Menu.........
1.push
2.pop
3.exit
Enter your choice 1-3:1
Enter the element to be push:4
After inserting the elements,stack is:
4
Enter your choice 1-3:1
Enter the element to be push:7
After inserting the elements,stack is:
7 4
Enter your choice 1-3:1
Enter the element to be push:4
What is the algorithm for reverse a given number recursively?
//Function that reverses a given number
int reverse(int num)
{
static int sum,base =1;
sum=0;
if(num>0)
{
reverse(num/10);
sum += (num%10)*base;
base*=10;
}
return sum;
}
What is the difference between simulation languages and high level languages?
simulator is an algorithm used to simulate the process of a system...
How do you write program c of table 5?
#include
#include
void main()
{ int a,i;
printf("\nThe Multiplication table of 5 is:\n");
for(i=1;i<=20;i++)
printf("%d",a*i);
getch();
}
It will print upto 20.
gets()
Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached.
The ending newline character ('\n') is not included in the string.
getchar()
Returns the next character from the standard input (stdin).
It is equivalent to getc with stdin as its argument. === ===
What are the functions of a special library?
mad people u dont have an answer an thats what i am looking for
Polymorphism in VC++ is the same as polymorphism in C++ itself. When you implicitly call a virtual method of a base class or one of its derivatives, then you rightly expect the most-derived override to execute instead, even when the runtime type is completely unknown to the caller and cannot be determined at runtime let alone compile time. You get that for free simply by overriding the known virtual methods of the base class, and without any need for expensive runtime type information, which is only useful if the caller is actually aware of the type in the first place, and which can only be predicted on a closed system. The whole point of polymorphism is that the base class (and therefore the caller) need know nothing whatsoever about any of its derivatives in order to execute more specialised methods. VC++ fully supports this aspect of OOP.
Program to find the sum and difference of two matrices?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10],m,n,i,j;
cout<<"Enter number of rows: ";
cin>>m;
cout<<"Enter number of coloumns: ";
cin>>n;
cout<<endl<<"Enter elements of matrix A: "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"Enter element a"<<i+1<<j+1<<": ";
cin>>a[i][j];
}
}
cout<<endl<<"Enter elements of matrix B: "<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"Enter element b"<<i+1<<j+1<<": ";
cin>>b[i][j];
}
}
cout<<endl<<"Displaying Matrix A: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl<<endl;
}
cout<<endl<<"Displaying Matrix B: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl<<endl;
}
cout<<endl<<"Matrix A + Matrix B = Matrix C: "<<endl<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]+b[i][j]<<" ";
}
cout<<endl<<endl;
}
getch();
}
C program to multiply two numbers without using arithmetic operator and using recursive function?
# include<stdio.h>
main()
{
int a,b,i:
int result=0;
printf("enter two numbers to be multipied");
scanf("%d%d",&A,&B);
for(i=1;i<=b;i++)
result=result+A;
printf("%d*%d=%d\n",a,b,result);
}
Yes, SQL is a high level language, since it allow us to get result without much going into Assembly level instructions, by using interpreter to change our statements/queries into machine level instructions!.
What is the difference between array and normal data type?
Data typing is static, but weakly enforced
What are the words that make up a high-level programming language called?
Some languages have specific terms, however keyword or reserved word is the general terminology we use when referring to a programming language's primary vocabulary. That is; words that cannot be used as identifiers. However, some languages also have contextual keywords. For instance, C++ has final and override contextual keywords. These can be used as both identifiers and keywords, depending on the context. The only reason for this is that people were using these words as identifiers before they were introduced to the language (in C++11) and making them actual keywords would have broken a lot of older code.
What is the difference between prefix and postfix increment operator in c plus plus?
Both the prefix and the postfix increment operators increment the operand. The difference is what is the value of the expression during the evaluation of the expression. In the prefix form, the value is already incremented. In the postfix form, it is not.
int a = 1;
int b = ++a;
// both a and b are now equal to 2
int a = 1;
int b = a++;
// a is equal to 2 and b is equal to 1
How to write a C program to generate Fibonacci series up to 10 elements and store in array?
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
int swap(int* , int*);
int main()
{
int c=0;
int b=1;
int max;
cout<<"Enter the maximum limit of fibbonacci series = ";
cin>>max;
if(max>0)
{
cout<<c<<endl;
}
else
{
exit (EXIT_FAILURE);
}
for(int i=0;i<max;i++)
{
c=b+c;
swap(&b,&c);
if(c<max)
{
cout<<c<<endl;
}
else
{
break;
}
}
getch();
return 0;
}
int swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
return z;
}
How to Convert binary to decimal in c?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,k,j,b[100];
clrscr();
printf("Enter a Number:");
scanf("%d",&n);
k=n;
for(i=0;i<=k;i++)
{
b[i]=n%2;
n=n/2;
if(n==0)break;
}
printf("\n\nBinary Equivalent:");
for(j=i;j>=0;j--)
printf("%d",b[j]);
getch();
}
Happy Coding...!!!
What do you mean by string in c language?
Character arrays or pointers to character are termed as strings in c language. Like:
char arr[10] = {'s', 't', 'i', 'n', 'g'};
char *pchar = "string";
Above answer is the first answer for the question
But there is a lot of difference between character array and string.
string means a group of characters .And string is enclosed between double quotation marks i.e(" ") .
Declaration of string is same as of char array ,which is as follows
char str[20];
And initialization is different from that of character array
initialization:-
char str[7]={"vardhan"};