Why should indexes be used instead of subscripts to identify array elements?
You cannot uses indices instead of subscripts. The subscript operator [] requires an index in order to determine the subscript. Even if you don't use the subscript operator you still need an index to determine the offset of the subscript. Indeed, the only time you do not need an index is when traversing the array using a roving pointer, which is arguably more efficient than using a subscript to traverse an array since subscripts use multiplication instead of the much simpler increment/decrement operation.
Relationship between actual and formal arguments?
The formal arguments are the names given to the parameters/arguments in the function declaration. These names will be used within the body of the function.
void myFunc( int i, char c ); // Function prototype
The actual arguments are the variables and/or constants (those supplied by the caller) that are used when invoking the function.
int intVar = 6;
char charVar = 'e';
// Actual parameters 3 and 'G' will be mapped to the
// formal parameters 'i' and 'c'
myFunc( 3, 'G' ); // Execute function
// Actual parameters 'intVar' and 'charVar' will be mapped
// to the formal parameters 'i' and 'c'
myFunc( intVar, charVar ); // Execute function
What is definition of file handling in c plus plus?
File is a place where information or data is stored.
we make use of some of the file-handling functions in c like:
fopen()-for opening a file.
fclose()-to close a file. Every file being opened for any operations like:
"r"- Read-Only mode.
"w"-Write-only mode.
"a"-append mode.
"r+"-read+write mode.
"w+"-write+read mode.
"a+"-read+append mode.
We should make use of FILE pointer ,in order to perform any such operations on the files.
There are many input and output functions used along with files.
fgetc()
fgets()
fscanf()
fputc()
fprintf()
fputs()
fseek()
rewind()
File handling is used to read or write a file without directly opening it.its contents are opened in another files by using above specified commands
in c++ programming for file handling we have to use a header file but in c noheader file regarding to file handling is required
How does 8 track continuous loop?
An 8-track tape is a magnetic audio storage format that uses a continuous loop of tape divided into four stereo programs, each lasting about 2-3 minutes. The tape moves continuously through the player, with a playback head that reads the magnetic signals on the tape. As one program ends, the player automatically switches to the next program by shifting the playback head to a different track. This design allows for uninterrupted listening without needing to rewind or change the tape manually.
How do you swap two variables using a third variable?
Let us take a=40,b=50.Now after swapping,we should get the output as a=50,b=40.
main()
{
int a=40,b=50;
a=a+b;
b=a-b;
a=a-b;
printf("a=%d,b=%d",a,b);
}
Compare Do-While with While Statements?
A Do-While loop looks like this:
do {
loop body
} while (condition);
and a While loop looks like this:
while (condition) {
loop body
}
The main difference is that the loop body is always run once in the Do-While loop, then the condition is checked to see if the loop should keep running. In a While loop, the condition is checked first, and it will not run the loop body at all if the condition is false.
The scope of a variable is the range, or area, in which a variable exists.
// this c is global and can be referenced from anywhere
int c = 1;
void foo() {
// this c is local to function foo and can't be referenced from the outside
int c = 2;
}
void bar() {
// if we try to reference c here, we get the value 1 from the global variable
}
What is high-level language compiled?
Compiling a high-level language means that the high-level source code (what is typed by the programmer) is being converted to machine code (binary, i.e. 0's and 1's) that the local computer can execute. In interpreter languages, the source code is first converted to byte code, which is either run directly (slower, but very portable), or interpreted by a virtual machine (see JVM) into machine code, and then executed (faster, but somewhat less portable). If an error is found the compilation stops and the errors are displayed.
default : <statement>;
i.e.
switch (value) {
case 1 : do_this(); break;
case 2 : do_that(); break;
default : do_whatever();
}
Simple c program for mergesort?
#include
#include
#define MAX_ARY 10
void merge_sort(int x[], int end, int start);
int main(void) {
int ary[MAX_ARY];
int j = 0;
printf("\n\nEnter the elements to be sorted: \n");
for(j=0;j scanf("%d",&ary[j]);
/* array before mergesort */
printf("Before :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);
printf("\n");
merge_sort(ary, 0, MAX_ARY - 1);
/* array after mergesort */
printf("After Merge Sort :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);
printf("\n");
getch();
}
/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[MAX_ARY];
if(end == start)
return;
mid = (end + start) / 2;
merge_sort(x, end, mid);
merge_sort(x, mid + 1, start);
for(j = 0; j < size; j++)
executing[j] = x[end + j];
mrg1 = 0;
mrg2 = mid - end + 1;
for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
else
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
}
}
;j++)>
What is the difference between an actual type and declared type variable?
I believe a declared type is the type used when first declaring a variable.
And the actual type is the type that the variable is actually assigned, which
could be the declared type or any subtype of that type. So given
GeometricObject shape = new Triangle();
GeometricObject is the declared type and Triangle is the actual type. To
demonstrate this more clearly the following statements are all correct.
GeometricObject shape; // shape declared to be a GeometricObject
shape = new Triangle(); // actual type assigned = Triangle
shape = new Square(); // now shape refers to a Square object
This is the basis of polymorphism. It allows you to write methods which have
for parameters more general types but are able to handle subtypes differently.
This makes your code more general and reusable.
What function enables the user to input information while the program is being executed in c?
The scanf() function is a commonly used function to input information during the execution of a program. scanf() function has its prototype in the stdio.h header file.
To accept and display a number:
int num;
printf("Enter a number: ");
scanf("%d",&num);
printf("You entered %d",num);
To accept and display a character, replace %d with %c and make num a character variable [char].
Plus: gets, fgets, read, fread, getchar, getc, fgetc, getch...
Is it possible to use a for loop in place of a while loop?
Yes.
while loop consist of only condition statement to make for loop look as while loop we can use syntax shown below:
for(;condition;)
eg:
for(;i<=n;)
What is the difference between string type and character type constants?
A character type constant is a character, a char, while a string type is an array of characters. The character type constant is one character and is delimited by single quotes. The string type constant is zero or more characters and is delimited by double quotes.
#include<iostream>
#include<iomanip>
#include<time.h>
template<typename T>
size_t find(T& data, T a[], size_t size)
{
size_t index=0;
do {
if(a[index]==data)
break;
} while(++index<size);
return(index);
}
template<typename T>
void print(T a[], size_t size)
{
using std::cout;
using std::endl;
using std::setw;
size_t index=0;
do{
if(index&&index%20==0)
cout<<endl;
cout<<setw(3)<<a[index];
}while(++index<size);
cout<<endl;
}
int main()
{
srand((unsigned)time(NULL));
const size_t size=100;
unsigned int a[size];
size_t index=0;
do{
unsigned int data=rand()%100;
do{
data=rand()%100;
} while(find(data,a,index)<index);
a[index]=data;
} while(++index<size);
print(a,size);
}