structures and files

Upload: murdochroy1

Post on 02-Mar-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 Structures and Files

    1/23

    TWO DIMENSIONAL DYNAMIC ARRAY

    Creating a dynamic 2D array using Dynamic Memory Allocation and

    accessing te elements using !ointers"#$itout using %&% and %'() and use te

    dynamic 2D array to *ind te trans!ose o* a matri+"#include

    #include

    int main(void)

    { int row,col,i,j;

    int **a;

    clrscr(); printf("nter row and column!");

    scanf("dd",row,col);

    a$(int **)calloc(row,si%eof(int)); for(i$&;i

  • 7/26/2019 Structures and Files

    2/23

    1etch();

    return &;

    Write a C !rogram to multi!ly t$o matrices using t$o dimensional dynamic

    arrays"You must cec, te correctness o* multi!lication rules $ile in!utting

    te si-e o* te matrices to .e multi!lied and also you must not use any o*/&/and /'0 sym.ols in your $ole !rogram code"#include

    #include

    int main(void)

    { int r2,r3,c2,c3,i,j,4;

    int **a2;

    int **a3; int **am;

    clrscr(); printf("nter the si%e of 2st matri0");

    scanf("dd",r2,c2); printf("nter the si%e of 3nd matri0");

    scanf("dd",r3,c3);

    if(c25$r3) {

    printf("6ron1 input555");

    else

    {

    a2$(int **)calloc(r2,si%eof(int)); for(i$&;i

  • 7/26/2019 Structures and Files

    3/23

    printf("opulate the 3nd matri0,+n");

    for(i$&;i

  • 7/26/2019 Structures and Files

    4/23

    #include

    #include

    #includestruct student

    {

    int roll;char name-8&;

    float mar4s;

    ;int main(void)

    {

    struct student s;

    sstem("clear");printf("nter 9oll!");

    scanf("d",(s.roll));

    printf("nter :ame!");

    scanf("s",(s.name));printf("nter ar4s!");

    scanf("f",(s.mar4s));printf("9oll$dn:ame$snar4s$3.3fn",s.roll,s.name,s.mar4s);

    return &;

    Creating a structure and accessing te structure elements using % ( Arro$

    o!erator"#include

    #include#include

    struct student

    {int roll;

    char name-8&;

    float mar4s;;

    int main(void)

    {

    struct student s;

    struct student *ptr;

    ptr$s;sstem("clear");

    printf("nter 9oll!");

    scanf("d",(ptr+>roll));

    printf("nter :ame!");scanf("s",(ptr+>name));

    printf("nter ar4s!");

    scanf("f",(ptr+>mar4s));

  • 7/26/2019 Structures and Files

    5/23

    printf("9oll$dn:ame$snar4s$3.3fn",ptr+>roll,ptr+>name,ptr+>mar4s);

    return &;

    Creating a structure and accessing te structure elements using

    !ointer#$itout using % ( arro$ o!erator)"

    #include

    #include#include

    struct student

    {int roll;

    char name-8&;

    float mar4s;

    ;int main(void)

    {

    struct student s;struct student *ptr;

    ptr$s;

    sstem("clear");printf("nter 9oll!");

    scanf("d",((*ptr).roll));

    printf("nter :ame!");

    scanf("s",((*ptr).name));printf("nter ar4s!");

    scanf("f",((*ptr).mar4s));

    printf("9oll$dn:ame$snar4s$3.3fn",(*ptr).roll,(*ptr).name,(*ptr).mar4s);return &;

    Creating a dynamic structure using dynamic memory allocation and accessing

    te structure elements using % ( arro$ o!erator"

    #include

    #include#include

    struct student{

    int roll;char name-8&;

    float mar4s;

    ;int main(void)

    {

  • 7/26/2019 Structures and Files

    6/23

    struct student *ptr;

    ptr$(struct student *)malloc(si%eof(struct student));sstem("clear");

    printf("nter 9oll!");

    scanf("d",(ptr+>roll));printf("nter :ame!");

    scanf("s",(ptr+>name));

    printf("nter ar4s!");scanf("f",(ptr+>mar4s));

    printf("9oll$dn:ame$snar4s$3.3fn",ptr+>roll,ptr+>name,ptr+>mar4s);

    return &;

    Creating a dynamic structure using dynamic memory allocation and accessing

    te structure elements using !ointer#$itout using % ( arro$ o!erator)"

    #include

    #include

    #include

    struct student{

    int roll;

    char name-8&;float mar4s;

    ;

    int main(void){

    struct student *ptr;ptr$(struct student *)malloc(si%eof(struct student));

    sstem("clear");

    printf("nter 9oll!");

    scanf("d",((*ptr).roll));printf("nter :ame!");

    scanf("s",((*ptr).name));

    printf("nter ar4s!");scanf("f",((*ptr).mar4s));

    printf("9oll$dn:ame$snar4s$3.3fn",(*ptr).roll,(*ptr).name,(*ptr).mar4s);

    return &;

    Creating te array o* structures and accessing te structure array elements

    using %"( Dot o!erator"#include

    #include

  • 7/26/2019 Structures and Files

    7/23

    #include

    struct student

    {int roll;

    char name-8&;

    float mar4s;;

    int main(void)

    {int i,n;

    struct student s-8&;

    sstem("clear");

    printf("nter the num7er of students!");scanf("d",n);

    for(i$&;i

  • 7/26/2019 Structures and Files

    8/23

    printf("nter the num7er of students!");

    scanf("d",n);

    for(i$&;iroll));printf("nter :ame d!",i'2);

    scanf("s",((s'i)+>name));

    printf("nter ar4s d!",i'2);scanf("f",((s'i)+>mar4s));

    for(i$&;iroll,(s'i)+>name,(s'i)+

    >mar4s);

    return &;

    Creating te array o* structures and accessing te structure array elements

    using !ointer #$itout using % ( Arro$ o!erator)"

    #include

    #include

    #include

    struct student{

    int roll;

    char name-8&;float mar4s;

    ;

    int main(void){

    int i,n;

    struct student s-8&;

    sstem("clear");printf("nter the num7er of students!");

    scanf("d",n);

    for(i$&;i

  • 7/26/2019 Structures and Files

    9/23

    for(i$&;i

  • 7/26/2019 Structures and Files

    10/23

    Creating a dynamic array o* structures using dynamic memory allocation and

    accessing te structure array elements using % ( Arro$ o!erator"

    #include#include

    #include

    struct student{

    int roll;

    char name-8&;float mar4s;

    ;

    int main(void){

    int i,n;

    struct student *s;

    sstem("clear");printf("nter the num7er of students!");

    scanf("d",n);

    s$(struct student *)malloc(n*si%eof(struct student));for(i$&;iroll));

    printf("nter :ame d!",i'2);

    scanf("s",((s'i)+>name));

    printf("nter ar4s d!",i'2);scanf("f",((s'i)+>mar4s));

    for(i$&;iroll,(s'i)+>name,(s'i)+>mar4s);

    return &;

    Creating a dynamic array o* structures and accessing te structure array

    elements using !ointer #$itout using %

    ( Arro$ o!erator)"#include

    #include#include

    tpedef struct student

    {int roll;

    char name-8&;

  • 7/26/2019 Structures and Files

    11/23

    float mar4s;

    1oru;

    int main(void)

    {

    int i,n;1oru *s;

    sstem("clear");

    printf("nter the num7er of students!");scanf("d",n);

    s$(1oru *)malloc(n*si%eof(1oru));

    for(i$&;i

  • 7/26/2019 Structures and Files

    12/23

    printf("=ile Bpened uccessfulln");

    fclose(fp);

    return &;

    5rogram to read a te+t *ile 7 dis!laying te content o* te *ile68#include

    #include

    #include#include

    int is=ile0ist(const char *file:ame)

    { =? *fp;

    if(fp$fopen(file:ame,"r"))

    {

    return 2;

    fclose(fp);

    else {

    return &;

    ;

    int main(void)

    {

    char file-8&; =? *fp;

    int count; char ch; printf("nter file name!");

    scanf("s",file);

    if(is=ile0ist(file)) {

    if(fp$fopen(file,"r"))

    { for(count$&;((ch$1etc(fp))5$B=)(5feof(fp));count'')

    {

    printf("c",ch);

    printf("nd characters read",count);

    fclose(fp);

    else

    {

    fputs("rror Bpenin1 =ile555",stderr); e0it(2);

  • 7/26/2019 Structures and Files

    13/23

    else {

    printf("=ile Coes not 0ist555");

    return &;

    5rogram to read a te+t *ile 7 dis!laying te content o* te *ile68#include

    #include

    #include#include

    #define DEFG 2&&

    int is=ile0ist(const char *file:ame)

    {

    =? *fp; if(fp$fopen(file:ame,"r"))

    { return 2;

    fclose(fp);

    else

    {

    return &;

    ;

    int main(void){ char file-8&;

    =? *fp;

    int count; char *str;

    printf("nter file name!");

    scanf("s",file); if(is=ile0ist(file))

    {

    if(fp$fopen(file,"r"))

    { str$(char *)calloc(DEFG,si%eof(char));

    for(count$&;5feof(fp);count'')

    { fscanf(fp,"s",str);

    printf("sc",str,1etc(fp));

    fclose(fp);

  • 7/26/2019 Structures and Files

    14/23

    else

    { fputs("rror Bpenin1 =ile555",stderr);

    e0it(2);

    else

    { printf("=ile Coes not 0ist555");

    return &;

    5rogram to read a te+t *ile 7 dis!laying te content o* te *ile68#include

    #include

    #include#include

    #define DEFG 2&&int is=ile0ist(const char *file:ame)

    {

    =? *fp; if(fp$fopen(file:ame,"r"))

    {

    return 2;

    fclose(fp);

    else { return &;

    ;int main(void)

    {

    char file-8&; =? *fp;

    int count;

    char *str;

    printf("nter file name!"); scanf("s",file);

    if(is=ile0ist(file))

    { if(fp$fopen(file,"r"))

    {

    str$(char *)calloc(DEFG,si%eof(char)); for(count$&;fread(str,si%eof(str),2,fp)$$25feof(fp);count'')

  • 7/26/2019 Structures and Files

    15/23

    {

    printf("s",str);

    fclose(fp);

    else {

    fputs("rror Bpenin1 =ile555",stderr);

    e0it(2);

    else

    { printf("=ile Coes not 0ist555");

    return &;

    5rogram to $rite te contents o* a dynamic array o* structure into a *ile" I*

    te *ile already e+ist a!!end te structure array contents6 create a ne$ *ile

    and sa9e te contents oter$ise"#include

    #include#include

    #include

    #define DEFG 2&&

    tpedef struct student

    { int roll; char name-8&;

    float mar4s;

    stud;

    int is=ile0ist(const char *file:ame)

    { =? *fp;

    if(fp$fopen(file:ame,"r"))

    {

    return 2; fclose(fp);

    else {

    return &;

    ;

  • 7/26/2019 Structures and Files

    16/23

    int main(void)

    {

    char file-8&$"e!HHstudent.t0t"; =? *fp;

    int count,i,n;

    stud *s; printf("nter the no of students!");

    scanf("d",n);

    if(is=ile0ist(file)) {

    if(fp$fopen(file,"a'"))

    {

    s$(stud *)calloc(n,si%eof(stud));

    for(i$&;i

  • 7/26/2019 Structures and Files

    17/23

    scanf("s",((*s).name));

    printf("nter mar4s d!",i'2);

    scanf("f",((*s).mar4s));

    for(i$&;i

  • 7/26/2019 Structures and Files

    18/23

    ;

    int main(void)

    { char file-8&$"e!HHstudent.t0t";

    =? *fp;

    int count,i,n; stud *s;

    char str-8&;

    printf("nter the no of students!"); scanf("d",n);

    if(is=ile0ist(file))

    {

    if(fp$fopen(file,"a'")) {

    s$(stud *)calloc(n,si%eof(stud));

    for(i$&;i

  • 7/26/2019 Structures and Files

    19/23

    printf("nter name d!",i'2);

    scanf("s",((*s).name));

    printf("nter mar4s d!",i'2); scanf("f",((*s).mar4s));

    for(i$&;i

  • 7/26/2019 Structures and Files

    20/23

    printf("nter name d!",i'2);

    scanf("s",((*(s'i)).name));

    printf("nter mar4s d!",i'2); scanf("f",((*(s'i)).mar4s));

    printf("tudent Cata7ase!n+++++++++++++++++n"); for(i$&;i

  • 7/26/2019 Structures and Files

    21/23

    {

    printf("tudent 9ecord#dn",i'2);

    printf("9oll!dn",(*(r'i)).roll); printf(":ame!sn",(*(r'i)).name);

    printf("ar4s!I.3fn",(*(r'i)).mar4s);

    HHDnother wa to perform this

    H*while(fread(r-count,si%eof(prana),2,fp)$$2)

    { count'';

    *H

    count$i;

    printf("=ile records$dn",count); printf("nter the no of students!");

    scanf("d",n);

    s$(prana *)calloc(n,si%eof(prana));

    if(s5$:@??) {

    for(i$&;i

  • 7/26/2019 Structures and Files

    22/23

    int roll;

    char name-8&;

    float mar4s; prana;

    int main(void)

    { int n,i,count$&;

    clrscr();

    =? *fp; prana *s;

    s$(prana *)calloc(DEF/@C:/,si%eof(prana));

    if((s$$:@??)JJ((fp$fopen("!student9ecord.7in","a'7"))$$:@??))

    { fputs("rror555",stderr);

    e0it(2);

    else {

    rewind(fp); for(i$&;fread(s'i,si%eof(prana),2,fp)$$2;i'')

    {

    printf("tudent 9ecord#dn",i'2); printf("9oll!dn",(*(s'i)).roll);

    printf(":ame!sn",(*(s'i)).name);

    printf("ar4s!I.3fn",(*(s'i)).mar4s);

    count$i;

    printf("nd e0istin1 records retrievedn",count);

    printf("nter the no of students!"); scanf("d",n);

    if(n

  • 7/26/2019 Structures and Files

    23/23

    fclose(fp); 1etch();

    return &;

    Write a C !rogram to read %:oo,Title( ;string