What are all the different methods of accessing array elements?
#include<iostream>
#include<cassert>
int main()
{
char str[] = "The quick brown fox jumped over the lazy dog.";
// Access the 5th element ('q' @ index 4) in 6 different ways:
char a = *(str+4);
char b = *(4+str);
char c = str[4];
char d = 4[str];
char e = *(&str[2]+2);
char f = *(2+&str[2]);
// Point to 7th element and access the 5th element:
char* p = &str[6];
char g = *(p-2);
// Test for equality:
assert(a==b);
assert(b==c);
assert(c==d);
assert(d==e);
assert(e==f);
assert(f==g);
assert(g=='q');
}
Explanation:
There is really just one version, a, but just as x+y == y+x, it is commutative thus we get version b as well. Note that the array name alone always returns a reference to the start of the array. Once we factor out all the pointer arithmetic, we are essentially just returning the dereferenced value of an address within the array.
Version c is the conventional notation using the subscript operator. However the subscript operator is merely sugar-coating (a notational convenience). Behind the scenes, we're actually executing a.
Version d is an unusual form that is rarely seen but is perfectly valid. Just as str[4] is functionally equivalent to *(str+4), so 4[str] is functionally equivalent to *(4+str), thus d is the same as b (which is the same as a).
Versions e and f are similar to a and b respectively, but instead of taking the address of the start of the array, we now take the address of the 3rd element (index 2) and then offset by 2 elements to get to the 5th element. In these versions, we can also use a negative offset to move backwards from an element, so long as we remain within the bounds of the array. These versions can also be notated more conventionally using version c; str[2+2] which is the same as a.
Version g is similar to e except we now store the address of the 7th element (index 6) in p and then use pointer arithmetic to obtain the offset of the 5th element. However, this version is not commutative so we cannot use *(2-p), but is functionally the same as a.
There are other variants similar to these 7 but they all ultimately come back to version a once you factor out all the pointer arithmetic. In other words, no method is better or worse than any other method. However version c is favoured for its readability alone, regardless of whether the offset is a compile-time constant or needs to be calculated at runtime. All others are academic but give a better understanding of what's really going on behind the scenes.
No. A function takes in values of no, one, or more input variables, and returns no or one result. It cannot return more than one result. Do not confuse this with returning multiple results using call by reference parameters - this is not the same thing.
malloc/calloc/realloc will return NULL
What is asm in sorting algorithms?
'ASM' is sort for Assembly, it has nothing to do with sorting algorithms.
How do you copy output of turbo c in Microsoft Word?
You cannot directly copy from Turbo editor and paste it in Word. But instead you can save your source file from Turbo. Then open that file in Notepad. From Notepad you can copy or paste to MS Word.
How do you write a C program to find row sum and column sum of a given matrix?
#include #include
This program takes in the number of rows (n) and columns (m) as well as the elements as a screen input in a matrix n x m.
It then calculates the sum of each row and each column and outputs it using the 'cout' command.
Also, if it is a square matrix, it calculates the sum of diagonal elements and prints it out.
Click here for SAMPLE INPUT 3 3 9 8 7 6 5 4 3 2 1
Click here for SAMPLE OUTPUT Matrix A, Row Sum(Last Column) and Column Sum(Last Row) : 9 8 7 24 6 5 4 15 3 2 1 6 18 15 12 Sum of diagonal elements is : 15
How do you tell your program you are using a VARIABLE?
You have to declare it. The simplest way to do so is by using the Dim keyword.
For example if you want a string variable called someString you would declare it thus:
Dim someString as string
It is exactly what it says it is: a pointer to a base class. The assumption is that you have an object to a derived class, but actually hold a pointer to its base class. The following minimal example demonstrates this:
class base{ public: virtual ~base(){} };
class derived: public base{};
int main()
{
base* p = new derived; // base class pointer to a derived instance.
delete( p );
return(0);
}
Note that derived has an "is-a" relationship with base (derived is a base), thus the above code is perfectly legal. Moreover, because the base class destructor is declared virtual, when you delete p you automatically destroy the instance of derived before the instance of base, thus ensuring a clean teardown (without a virtual destructor, a dangling reference to derived would be left behind, which will only lead to problems further down the line).
Taking things further, calling any virtual methods upon the base class automatically invokes the override in your derived class, thus ensuring that your derived class behaves accordingly, polymorphically, even though you only hold a pointer to the base class. In other words, the base class provides a generic interface that is common to all its derivatives, and you can call those generic methods via the base class pointer without ever needing to know the actual derived type.
Remember that base classes should never know anything about their derivatives since a new derivative could be created at any time in the future and would therefore be impossible to predict in advance. But so long as the derivative makes use of the virtual functions (the generic interface) provided by the base class, there is never any need to know the actual type. The derivative's own v-table takes care of that for you, thus completely eliminating the need for expensive runtime type information and dynamic downcasts (which is always a sign of poor class design). This then makes it possible for your derived class overrides to call non-generic methods, thus extorting non-generic behaviour from what is essentially a generic, base class pointer.
An ultrasound scanner is a scanning machine to do with sound. As the ultrasound scanner moves back and forth over your body, it sends sound waves through your skin and muscle. The sound waves are then turned into images that appear on TV screens, and can also be copied onto paper or X-ray film.
How do you draw a circle in c program?
Here, maybe a few other shapes as well
#include
#include
void main()
{
int gd=DETECT, gm;
int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 };
initgraph(&gd, &gm, "");
circle(100,100,50);
outtextxy(75,170, "Circle");
rectangle(200,50,350,150);
outtextxy(240, 170, "Rectangle");
ellipse(500, 100,0,360, 100,50);
outtextxy(480, 170, "Ellipse");
line(100,250,540,250);
outtextxy(300,260,"Line");
sector(150, 400, 30, 300, 100,50);
outtextxy(120, 460, "Sector");
drawpoly(6, poly);
outtextxy(340, 460, "Polygon");
getch();
closegraph();
}
C program to remove all comments from a c program?
/ sub.c Copyright 2009 vishnuprathish This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. / #include int main(int argc, char** argv) { FILE *fp; fp=fopen("sub.c","r"); char ch; ch=getc(fp); while(ch!=EOF) { if(ch=='/') { ch=getc(fp); if(ch=='/') { while((ch=getc(fp))!='\n') { getc(fp);//This commment wil be removed } } if(ch=='*')/*This also wil be removed*/ { while(1) { ch=getc(fp); if(ch=='*') { ch=getc(fp); if(ch='/') { break; } } } } } printf("%c",ch); ch=getc(fp); } return 0; }
Why you have to declare variable first in turbo c?
All variables (and constants) must be declared before they can be used. This is so the compiler knows exactly how much memory to allocate to the variable, as the declaration tells the compiler exactly what the variable's type is.
What is the difference between null pointer ASCII null character and null string?
A null pointer is a pointer which does not point to any valid memory location, and usually contains the binary value "0" to represent this (this is language dependent). The ASCII null character is a character-sized zero value (in ASCII, it is an unsigned byte with a value of 0), and typically represents the end of a string (esp. as in C and C++). A null string is one that is zero characters of usable string data; in a length-based string, this means the length parameter is set to 0, and in an ASCII null-terminated string, means the first character is set to 0.
What are the wildcard characters in C?
The wildcard characters in C programming include the asterisk (*) and the question mark (?). An asterisk stands for any missing number of characters in a string while a question mark represents exactly one missing character.
Nintendo is in Japanese like NREIALKEN GFRTAO and Carue is in Espan'ol like YURKO WILF as high level of Nintendo and Carue of something.
C programming is called middle level language?
(i) it gives or behaves as High Level Language through Functions - gives a modular programming and breakup, increased efficiency for reusability
(ii)it gives access to the low level memory through Pointers. Moreover it does support the Low Level programming i.e, Assembly Language.
As its a combination of these two aspects, its neither a High Level nor a Low level language but a Middle Level Language.
Of note: C++ supports pointers and some basic assembly aspects. It is, however, high-level. C is 3rd generation, not due to pointers or functions, as most languages after the 1st generation include some implementation of these, but because it introduced the first (relatively speaking) aspects of object orientation (structs and enums). C++ carried on with this, leading to the "4th", which has become too varried to refer to as such. There is no such thing as a middle-level language. Machine code to BASIC to C to C++ and Java and such, C is definitively on the higher end of the programming specture.
What are the c-programs for file allocation techniques?
// Indexed Allocation
#include
#include
#include
struct node {
int file_name;
int data;
int is_free;
int size;
int directory;
int cnt;
struct node* link;
struct node* inner_link[30];
};
struct node* defaultFile() {
struct node* temp = (struct node*)malloc( sizeof(struct node) );
temp->link = NULL;
temp->is_free=0;
temp->data='a';
temp->file_name=99;
temp->directory = 1;
return temp;
}
struct node* insert(struct node *rt, int size, int fname) {
struct node* temp = rt;
struct node* first=NULL,*last=NULL,*tt=NULL;
int flag=0,act_size=0, inner_fname=100,cnt=0;
last=first;
act_size = size;
if ( size>temp->size ) {
printf("There is not enough space on the disk to write that file\n");
return rt;
}
while( temp->link ) {
temp = temp->link;
}
first = defaultFile();
while( act_size>0 ) {
tt = defaultFile();
if (act_size>50)
tt->size = 50;
else
tt->size = act_size;
tt->file_name = inner_fname;
first->inner_link[cnt] = tt;
tt->directory = 0;
tt->is_free = 0;
act_size -= 50;
inner_fname += 1;
cnt += 1;
}
temp->link = first;
first->is_free = 1;
first->cnt = cnt;
first->size = size;
first->file_name = fname;
act_size = rt->size;
rt->size = act_size-size;
return rt;
}
void printFiles(struct node* rt) {
struct node *temp = rt, *tt;
int first=0,cnt=0;
printf("format is (File name, size)\n");
printf("\t(%d,%d)\n",rt->file_name,rt->size);
while( temp ) {
if ( temp->is_free ) {
printf("\t(%d,%d)\n",temp->file_name,temp->size);
first = 0;
while( cntcnt ) {
tt = temp->inner_link[cnt];
printf("\t\t(%d,%d)\n",tt->file_name, tt->size);
cnt += 1;
}
printf("\n");
}
temp = temp->link;
}
}
struct node* combine(struct node *rt,int fname) {
struct node *temp=rt,*nt=NULL,*temp1=temp->link;
int size=0;
if ( rt->file_name==fname ){
printf("You cannot that file as thats just to show that that much amount of space is left in the disk\n");
return rt;
}
while( temp1 ) {
if (temp1->is_free==0 && temp1->file_name==fname ) {
size = temp1->size;
temp->link = temp1->link;
temp1 = temp->link;
}
else {
temp = temp1;
temp1 = temp1->link;
}
}
rt->size += size;
return rt;
}
struct node* deleteFiles(struct node* rt, int fname) {
struct node *temp = rt,*nt=NULL;
int flag=0;
while( temp && flag==0 ) {
if (temp->file_name==fname) {
temp->is_free=0;
flag=1;
}
temp = temp->link;
}
if( flag==0 ){
printf("There doesnt exist any file with that name\n");
}
return combine(rt,fname);
}
int main() {
int flag,no,size,data;
struct node *root;
root = defaultFile();
root->size=1000;
data=100;
flag=no=size=0;
while( flag==0 ) {
printf("Enter no's \n1.insert\n 2.Delete\n 3.Print files \n 4.Exit\n");
scanf("%d",&no);
printf(" no is %d\n",no);
switch(no) {
case 1:
printf("Enter file size\n");
scanf("%d",&size);
root = insert(root, size, data);
data = data+1;
break;
case 2:
printf("Enter file name to delete\n");
scanf("%d",&size);
root = deleteFiles(root, size);
break;
case 3:
printFiles(root);
break;
case 4:
flag=1;
printf("Quitting from loop\n");
break;
default:
printf("Enter a valud no \n");
break;
}
}
}
How can we read two strings using c program?
#include,stdio.h>
main()
{
char string1[20],string2[20]
printf("enter the first string");
scanf("%s",string1);// reading the string1
printf("enter the second string");
scanf("%s", string2);// reading the the string2
printf( "the first string is %s",string1);// printing the string1
printf("the second string is %s",string2);// printing the string2
}
the problem of using scanf is that it does not take space. so we can use gets for it.
ie instead of scanf("%s",string1); we can use gets(string1); .
Can a machine having 64MB run an executable which is 300MB using far pointers?
Yes, but you will incur a substantial penalty in virtual address page fault rates. Also, the standard page overcommit ratio in Windows is 4 to 1, so you would only be able to have a 256MB address space.
How do you find missing numbers in a array with elements below its size?
Please rephrase your question. An array usually has a fixed size and I don't recall ever having to "go below its size". This implies that the missing elements are not within the range of the array.