Please help me to solve the following program in C++ array. After compile message appears "Possible use of "i" before definition.

#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]<<" ";
}

Thank you in advance.

Recommended Answers

All 13 Replies

...
void selsort(int AR[],int size)
{
int small,pos,tmp,i;
for(int i=0;i<size;i++)
{
....

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

...
void selsort(int AR[],int size)
{
int small,pos,tmp;
for(int i=0;i<size;i++)
{
...

Hope You'll find this information helpful.

C++ Syntax

Thanks for your reply but i could not get the resolve. Need help.

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.

#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]<<" ";
}

#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;
}
selsort(AR,N);
cout<<"\n\n The sorted array is as shown below.....\n";
for(int i=0;i<N;i++)
cout<<AR<<" ";
cout<<endl;
cin>>z;
}
void selsort(int AR[],int size)
{
int small,pos,tmp,i;
for(int i=0;i<size;i++)
{
small=AR; pos=i;
for(int j=(i+1);j<size;j++)
{
if(AR[j]<small)
{
small=AR[j];
pos=j;
}
}
}
tmp=AR;
AR=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

plz write a program for rational numbers in oop,that can give sum,difference,product n division of two rational numbers.

for(int i=0;i<N;i++)

cout<<AR[i]<<" ";

cout<<endl;

cin>>z;

}

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

//k0ns3rv

1.
write a 'c' program to read 4(four)numbers from a file 'BANK' and calculate the average of the numbers.Now print the calculated average in another output file 'AVERAGE'
2.
write a 'c' program that finds the sum and average of inputted five integer numbers of the array using dynamic memory allocation function malloc().
3.
write a 'c' program to create simple elements 1,2,3,4 in the link list of 4(four)nodes and display the list's elements.
4.
write a 'c' program to convert the expression (A+B)/(C+D) into postfix expression into stack.and then evaluate it for A=10,B=20,C=15,D=5 and display the stack status after each operation.
5.
write a 'c' programto create a linked list implemented on an array containing the following numbers:1,2,3,3,3,4,4,9
and pack it to remove the duplicate numbers.so that only the following data are contained by the nodes:1,2,3,4,9

commented: Unrelated thread hijack and "gimmetehcodez" lazy sponger -4

Please help me to solve the following program in C++ array. After compile message appears "Possible use of "i" before definition.

#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]<<" ";
}

Thank you in advance.

hi.on line 24 "int small,pos,tmp,i;" innitiate i to 0 and you are good to go

"int small, pos,tmp,i=0; "

I'm sure Ahmed and Vicky had been sitting in front of their computers for two years just waiting for your response. Good Job.

celsius Temperature Table
the formula for converting a temperature from Fahrenheit to celsius is C=5/9(f-32)
where F is the Fahrenheit temperature and C is the Celsius temperature .write a function named celsius that accepts a Fahrenheit temperature as an argumet.
the funtion should return the temperature, converted to Celsius. Demonstrate the function by calling it in a loop that displaysa table of the fahrenheut temperatures 0 through 20 and thier Celsius equivalents

considerd on array A(1.5,-5.5,10.15) stored in column mojor order .find the address of element A (3,3,13) when the base address of A is 500 and there are four memery location per word

Please start a new thread with each new question.

If you are wishing for help with an 'home-work' problem ... you firstly NEED to try to code a solution yourself ...

AND THEN (and only then) ..

submit what you have coded ... along with a description of your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.