Solved

Some Questions.

Posted on 2000-04-26
4
176 Views
Last Modified: 2010-04-15
1) How to account the length of ''ab\n\\012\\\''''?
2) What's the principle of the following programmes?
a: #include<stdio.h>
   main(){
      char a[]="labchmfye",t;
      int i,j;
      for(i=0;i<7;i+=2)
         for(j=i+2;j<a;j++)
            if(a[i]>a[j]){
               t=a[i];
               a[i]=a[j];
               a[j]=t;
               j++;
            }
     puts(a); printf("\n");}
-------------
b: ...
   int a[]={4,0,2,3,1},i,j,t;
   for (i=1;i<5;i++){
      t=a[i];
      j=i-1;
      while(j>=0&&t>a[j]){
         a[j+1]=a[j];
         j--;
      }
      a[j+1]=t;
   }
  ....
-----------
c: Is there anything wrong?
   main(){
   int a[10]={0};
     int i;
     for(i=0;i<3;i++)
        scanf("%d",&a[i]);
     for(i=1;i<10;i++)
        a[0]=a[0]+a[i];
     printf("%d\n",a[0]);
   }
------------
d:  Are these correct "int c[]={0}", "int c[10]={}",
"int a[][3]={{1,0,1},{},{1,1}}?

and how they work?
0
Comment
Question by:ahha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 2751615
Experts exchange user agreement specifically requires academic honesty from all of its users. So we can't do your homework for you. We'd be happy to answer specific questions, especially if they show you have done some work/thinking on your own, much as a TA might.
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2751666
hint for #1) - look for escape sequences in the string and don't count those in the length computation.

hint for #2a,b) - hand-trace the code and write intermediate results for the variables and counters as you go.

hint for #2c, d) - look in your book for array initialization syntax rules.


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2754175
1) How to account the length of ''ab\n\\012\\\''''?

when you see a '\', if the next char is an octal digit, then keep reading numbers, otherwise just ignore the next character.  In either case treat the whole thing as a single character.

2) What's the principle of the following programmes?
a: #include<stdio.h>
   main(){
      char a[]="labchmfye",t;
      int i,j;
      for(i=0;i<7;i+=2)
         for(j=i+2;j<a;j++)
            if(a[i]>a[j]){
               t=a[i];
               a[i]=a[j];
               a[j]=t;
               j++;
            }
     puts(a); printf("\n");}

looks like some sort of sorting algorithm .. what inidicate that is the comparison of values in the if statement and the exchanging of values (using temporary variable t).

However, there appears to be a bug in it because it is comparing j (a simple integer) with a (the address of an array of characters).

b: ...
   int a[]={4,0,2,3,1},i,j,t;
   for (i=1;i<5;i++){
      t=a[i];
      j=i-1;
      while(j>=0&&t>a[j]){
         a[j+1]=a[j];
         j--;
      }
      a[j+1]=t;
   }
  ....


this also looks like a sort .. I'll leave it to you to work out the type of sort.

c: Is there anything wrong?
   main(){
   int a[10]={0};
     int i;
     for(i=0;i<3;i++)
        scanf("%d",&a[i]);
     for(i=1;i<10;i++)
        a[0]=a[0]+a[i];
     printf("%d\n",a[0]);
   }

how many numbers do you read in?  How many are added up?  It isn't wrong per se (cannot determine what is right or wrong unless one knows what problem the program was meant to solve) .. but does seem strange.

d:  Are these correct "int c[]={0}", "int c[10]={}",
"int a[][3]={{1,0,1},{},{1,1}}?

read your C-syntax ... or try them in your compiler and find out !!  HINT: Only one of them is ok.


0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 2754213
actually .. that sounds like good enough for an answer to me .. but not too much help to be doing the work for you.

If you have any particular problems, please feel free to post comments here to discuss them ... but don't try to get us to do your homework for you !!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL handling single and double quotes 3 99
Finding a good hash function 4 130
Handling string inputs in C/Linux 23 195
Intel debugger, idb, only seeing .c and .h, but no .f90 1 476
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question