Please help me to solve C++ program in array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2007
Posts: 9
Reputation: Ahmed Padela is an unknown quantity at this point 
Solved Threads: 0
Ahmed Padela Ahmed Padela is offline Offline
Newbie Poster

Please help me to solve C++ program in array

 
0
  #1
Feb 1st, 2008
Please help me to solve the following program in C++ array. After compile message appears "Possible use of "i" before definition.
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void selsort(int [],int);
  4. void main()
  5. {
  6. int AR[50],N,z;
  7. clrscr();
  8. cout<<"How many elements do U want to create array with?(max.50).....";
  9. cin>>N;
  10. cout<<"\n Enter Array elements.....\n";
  11. for(int i=0;i<N;i++)
  12. {
  13. cin>>AR[i];
  14. }
  15. selsort(AR,N);
  16. cout<<"\n\n The sorted array is as shown below.....\n";
  17. for(int i=0;i<N;i++)
  18. cout<<AR[i]<<" ";
  19. cout<<endl;
  20. cin>>z;
  21. }
  22. void selsort(int AR[],int size)
  23. {
  24. int small,pos,tmp,i;
  25. for(int i=0;i<size;i++)
  26. {
  27. small=AR[i]; pos=i;
  28. }
  29. for(int j=(i+1);j<size;j++)
  30. {
  31. if(AR[j]<small)
  32. {
  33. small=AR[j];
  34. pos=j;
  35. }
  36. }
  37. tmp=AR[i];
  38. AR[i]=AR[pos];
  39. AR[pos]=tmp;
  40. cout<<"\n Array after pass-"<<(i+1)<<"-is:\n";
  41. for(int j=0;j<size;j++)
  42. cout<<AR[j]<<" ";
  43. }
Thank you in advance.
Last edited by Ancient Dragon; Feb 4th, 2008 at 1:04 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 114
Reputation: zhelih has a little shameless behaviour in the past 
Solved Threads: 11
zhelih's Avatar
zhelih zhelih is offline Offline
Junior Poster

Re: Please help me to solve C++ program in array

 
0
  #2
Feb 1st, 2008
  1. ...
  2. void selsort(int AR[],int size)
  3. {
  4. int small,pos,tmp,i;
  5. for(int i=0;i<size;i++)
  6. {
  7. ....

if you declared i before, you don't need to do it in the loop.

  1. ...
  2. void selsort(int AR[],int size)
  3. {
  4. int small,pos,tmp;
  5. for(int i=0;i<size;i++)
  6. {
  7. ...

Hope You'll find this information helpful.
An Apple a Day keeps a Doctor away!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 9
Reputation: Ahmed Padela is an unknown quantity at this point 
Solved Threads: 0
Ahmed Padela Ahmed Padela is offline Offline
Newbie Poster

Re: Please help me to solve C++ program in array

 
0
  #3
Feb 1st, 2008
C++ Syntax

Thanks for your reply but i could not get the resolve. Need help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1
Reputation: soil is an unknown quantity at this point 
Solved Threads: 0
soil soil is offline Offline
Newbie Poster

Re: Please help me to solve C++ program in array

 
0
  #4
Feb 1st, 2008
Your error is you did not define "i" out of for loops and u are trying to use it before defining it. When u define a variable (like i) in a for loop it's scope finishes at the end of for loop.

You must define an "i" out of for loop to use it after.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1
Reputation: anand.vicky24 is an unknown quantity at this point 
Solved Threads: 0
anand.vicky24 anand.vicky24 is offline Offline
Newbie Poster

Re: Please help me to solve C++ program in array

 
0
  #5
Feb 2nd, 2008
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void selsort(int [],int);
  4. void main()
  5. {
  6. int AR[50],N,z;
  7. clrscr();
  8. cout<<"How many elements do U want to create array with?(max.50).....";
  9. cin>>N;
  10. cout<<"\n Enter Array elements.....\n";
  11. for(int i=0;i<N;i++)
  12. {
  13. cin>>AR[i];
  14. }
  15. selsort(AR,N);
  16. cout<<"\n\n The sorted array is as shown below.....\n";
  17. for(int i=0;i<N;i++)
  18. cout<<AR[i]<<" ";
  19. cout<<endl;
  20. cin>>z;
  21. }
  22. void selsort(int AR[],int size)
  23. {
  24. int small,pos,tmp,i;
  25. for(int i=0;i<size;i++)
  26. {
  27. small=AR[i]; pos=i;
  28. for(int j=(i+1);j<size;j++)
  29. {
  30. if(AR[j]<small)
  31. {
  32. small=AR[j];
  33. pos=j;
  34. }
  35. }
  36. }
  37. tmp=AR[i];
  38. AR[i]=AR[pos];
  39. AR[pos]=tmp;
  40. cout<<"\n Array after pass-"<<(i+1)<<"-is:\n";
  41. for(int j=0;j<size;j++)
  42. cout<<AR[j]<<" ";
  43. }
Last edited by Ancient Dragon; Feb 4th, 2008 at 1:06 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 252
Reputation: technogeek_42 has a little shameless behaviour in the past 
Solved Threads: 3
technogeek_42's Avatar
technogeek_42 technogeek_42 is offline Offline
Posting Whiz in Training

Re: Please help me to solve C++ program in array

 
0
  #6
Feb 4th, 2008
Originally Posted by anand.vicky24 View Post
#include<iostream.h>
#include<conio.h>
void selsort(int [],int);
void main()
{
int AR[50],N,z;
clrscr();
cout<<"How many elements do U want to create array with?(max.50).....";
cin>>N;
cout<<"\n Enter Array elements.....\n";
for(int i=0;i<N;i++)
{
cin>>AR[i];
}
selsort(AR,N);
cout<<"\n\n The sorted array is as shown below.....\n";
for(int i=0;i<N;i++)
cout<<AR[i]<<" ";
cout<<endl;
cin>>z;
}
void selsort(int AR[],int size)
{
int small,pos,tmp,i;
for(int i=0;i<size;i++)
{
small=AR[i]; pos=i;
for(int j=(i+1);j<size;j++)
{
if(AR[j]<small)
{
small=AR[j];
pos=j;
}
}
}
tmp=AR[i];
AR[i]=AR[pos];
AR[pos]=tmp;
cout<<"\n Array after pass-"<<(i+1)<<"-is:\n";
for(int j=0;j<size;j++)
cout<<AR[j]<<" ";
}
you're "i" has a multiple declaration.. please check it
╞═══════╣SPYnX███
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: ana_moeen@ymail is an unknown quantity at this point 
Solved Threads: 0
ana_moeen@ymail ana_moeen@ymail is offline Offline
Newbie Poster

OOP

 
0
  #7
Oct 8th, 2009
plz write a program for rational numbers in oop,that can give sum,difference,product n division of two rational numbers.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 49
Reputation: K0ns3rv is an unknown quantity at this point 
Solved Threads: 6
K0ns3rv K0ns3rv is offline Offline
Light Poster
 
0
  #8
Oct 8th, 2009
  1.  
  2. for(int i=0;i<N;i++)
  3.  
  4. cout<<AR[i]<<" ";
  5.  
  6. cout<<endl;
  7.  
  8. cin>>z;
  9.  
  10. }

This part of the code is missing a curly bracket that might be it..
line 17.

//k0ns3rv
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC