help in theory Questions-Arrays

Java programming topics
Post Reply
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

help in theory Questions-Arrays

Post by Face » Fri Aug 06, 2010 11:24 pm

Code: Select all

class Paper200823{
public static void main(String args[]){
char a[]={o , o , h}; 
char b[]={k , s , a};
char c[]= new char[6];
 for (int i=0,k=0,m=0;i<=c.length-1; ++i)
{
    if(i%2 == 0){
          c[i] = a[k];
                k++;
    }else{
          c[i] = b[m];
        m++;
          }
}
for (int i=0;i<=c.length-1; ++i)
  System.out.print(c[i]);
  }
}
In this code we have errors.there are two errors.I found one..char a[]={o , o , h}; ..other line is char b[]={k , s , a};..

This is a theory question I found in a book.I can understand about the first error.In array a.but I can't understand the error of array b...can you please tell me about the second error in char b[]={k , s , a}; line..

friend,may be these are some odd question to this forum.because these are theory based questions.I am asking this because Expertcore is the one & only place I better know.:).& with trusted friends.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: help in theory Questions-Arrays

Post by Face » Fri Aug 06, 2010 11:34 pm

brother can you correct this for me...!

Code: Select all

class Paper200825{
public static void main(String args[]){
int ar[]= new int[3],sum=0;

for(int k=0 ; k<=ar.length ; k++)
  ar[k]=k+1;
for(int r=0 ; r<=ar.length ; r++)
     sum+=ar[k];
System.out.print(sum);
        }
}
This line has the error.sum+=ar[k];...i want to Add sum+k..

when I compile this in a Java editor I got these errors.

Code: Select all

>javac Paper200825.java
Paper200825.java:8: cannot find symbol
symbol  : variable k
location: class Paper200825
sum+=ar[k];
        ^
1 error
>Exit code: 1
can you tell me what is the error..?.I have lot of questions now.may be you will get fed up with answering form me!! ;)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: help in theory Questions-Arrays

Post by Neo » Sat Aug 07, 2010 12:59 am

G-sparkZ wrote:

Code: Select all

class Paper200823{
public static void main(String args[]){
char a[]={o , o , h}; 
char b[]={k , s , a};
char c[]= new char[6];
 for (int i=0,k=0,m=0;i<=c.length-1; ++i)
{
    if(i%2 == 0){
          c[i] = a[k];
                k++;
    }else{
          c[i] = b[m];
        m++;
          }
}
for (int i=0;i<=c.length-1; ++i)
  System.out.print(c[i]);
  }
}
In this code we have errors.there are two errors.I found one..char a[]={o , o , h}; ..other line is char b[]={k , s , a};..

This is a theory question I found in a book.I can understand about the first error.In array a.but I can't understand the error of array b...can you please tell me about the second error in char b[]={k , s , a}; line..

friend,may be these are some odd question to this forum.because these are theory based questions.I am asking this because Expertcore is the one & only place I better know.:).& with trusted friends.
Simple problem here. You need to put single quotes before and after a character to make it a char variable. Otherwise the compiler will try to find variables in those names.

char a[]={'o' , 'o' , 'h'};
char b[]={'k' , 's' , 'a'};
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: help in theory Questions-Arrays

Post by Neo » Sat Aug 07, 2010 1:06 am

G-sparkZ wrote:brother can you correct this for me...!

Code: Select all

class Paper200825{
public static void main(String args[]){
int ar[]= new int[3],sum=0;

for(int k=0 ; k<=ar.length ; k++)
  ar[k]=k+1;
for(int r=0 ; r<=ar.length ; r++)
     sum+=ar[k];
System.out.print(sum);
        }
}
This line has the error.sum+=ar[k];...i want to Add sum+k..

when I compile this in a Java editor I got these errors.

Code: Select all

>javac Paper200825.java
Paper200825.java:8: cannot find symbol
symbol  : variable k
location: class Paper200825
sum+=ar[k];
        ^
1 error
>Exit code: 1
can you tell me what is the error..?.I have lot of questions now.may be you will get fed up with answering form me!! ;)
When you define a variable within a loop {like for(int ABC.....}, ABC only valid witihn the loop. Not outside of it.

I'm putting brakets to clear out the scope of each loop.

Code: Select all

for(int k=0 ; k<=ar.length ; k++){
  ar[k]=k+1;
}

for(int r=0 ; r<=ar.length ; r++){
     sum+=ar[k];                            // you can use k here as the k only valid in the previous loop
}
If k is defined outside the loop (before first loop) then it is valid for both loops.

Code: Select all

int k;

for(k=0 ; k<=ar.length ; k++){
  ar[k]=k+1;
}

for(int r=0 ; r<=ar.length ; r++){
     sum+=ar[k];                            // k is valid here
}
However, according to the way the program is written, I suspect sum += ar[k]; must be sum += ar[r] where r is valid in the second loop ;)
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: help in theory Questions-Arrays

Post by Face » Sat Aug 07, 2010 2:41 pm

In my first question It was real simple there.It was a question in a text book.Thanks for the help.It is really good explanation for me.thanks BRO NEO..

I am studding the next answer you post....
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: help in theory Questions-Arrays

Post by Face » Sat Aug 07, 2010 2:56 pm

Ahaa BRO I got the Idea..My "k" variable can not be use in second loop.Because it is only work with the first loop.It can not be use in the second loop.If I define the k in the begging on the for program(Outside the loops)then I can use it in the whole program.If I define a variable in side the loop,I can work with that variable inside the loop only. :lol:

Is it correct BRO...I think I got a clear picture of the problem in that program.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: help in theory Questions-Arrays

Post by Neo » Sun Aug 08, 2010 12:40 am

There are two types of variables. Local and Global.

Local variable is a variable that is given local scope. Such a variable is accessible only from the function or block in which it is declared.

Global variable is a variable that is accessible in every scope.


In your case, k was a local variable for the "for" block and only accessible within that.
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: help in theory Questions-Arrays

Post by Face » Sun Aug 08, 2010 8:15 am

aha...yep...k is a local variable....that is the reason for my error...thank you for the theory knowledge..
Post Reply

Return to “Java Programming”