| | |
c++ arrays, help identify element place
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 4
Reputation:
Solved Threads: 0
I have to identify which element in the array the highest and lowest numbers are. I have finished the rest of the project but you can see that i need help with the element position. Here's what i have and thank you, thank you, thank you
#include <iostream.h>
#include"apvector.h"//didn't inclue io manipulators because working with int
int main()
{
int high;
int low; //declare variables
int sum;
int average;
int num_values;
int index;
high=1; //initialize variables
low=1;
sum=1;
cout<<"Please enter a number. "<<endl;//get number of spaces in array
cin>> num_values;
cout<<"Enter values for the array of "<<num_values<<" items."<<endl;
apvector<int>value(num_values); //declare array
for(index = 0; index <= (num_values - 1); index++)//loop to get values to
//fill array
{
cout << "Enter the number for space " << index + 1 << ": ";
cin >> value[index]; //input of values to fill array
if (value[index]>high)
{high=value[index];} //assign new values to low and high
if (value[index]<low)
{low=value[index];}
sum=sum+value[index]; //update sum for average
}
average=sum/num_values; // get average
cout<< "the average of the numbers is " <<average<<endl; //print out results
cout<<"the largest number is " <<high<<
" and its element position is " <<index <<endl;//can't figure out how
cout<<"the smallest number is " <<low<< //to show the # of element
" and its element position is " <<index <<endl; //that the high and low
//occupy
system("pause");//pause screen to read input
return 0;
}
#include <iostream.h>
#include"apvector.h"//didn't inclue io manipulators because working with int
int main()
{
int high;
int low; //declare variables
int sum;
int average;
int num_values;
int index;
high=1; //initialize variables
low=1;
sum=1;
cout<<"Please enter a number. "<<endl;//get number of spaces in array
cin>> num_values;
cout<<"Enter values for the array of "<<num_values<<" items."<<endl;
apvector<int>value(num_values); //declare array
for(index = 0; index <= (num_values - 1); index++)//loop to get values to
//fill array
{
cout << "Enter the number for space " << index + 1 << ": ";
cin >> value[index]; //input of values to fill array
if (value[index]>high)
{high=value[index];} //assign new values to low and high
if (value[index]<low)
{low=value[index];}
sum=sum+value[index]; //update sum for average
}
average=sum/num_values; // get average
cout<< "the average of the numbers is " <<average<<endl; //print out results
cout<<"the largest number is " <<high<<
" and its element position is " <<index <<endl;//can't figure out how
cout<<"the smallest number is " <<low<< //to show the # of element
" and its element position is " <<index <<endl; //that the high and low
//occupy
system("pause");//pause screen to read input
return 0;
}
Declare 2 more variables
int max_pos=0, min_pos=0;
................
if (value[index]>high)
{
high=value[index];
max_pos = index;
} //assign new values to low and high
if (value[index]<low)
{
low=value[index];
min_pos = index;
}
.....................
cout<<"the largest number is " <<high<<
" and its element position is " <<max_pos <<endl;
cout<<"the smallest number is " <<low<<
" and its element position is " <<min_pos <<endl;![]() |
Similar Threads
- Code Snippet: Searched element's place in an array (Pascal and Delphi)
- Merging 2 Arrays (JavaScript / DHTML / AJAX)
- first time using arrays (Java)
- Dynamic Declaration of Arrays Keys (PHP)
- returning three dimensional array (C)
- Comparing an element in an array (C++)
- Arrays problem (C++)
- arrays (Java)
Other Threads in the C++ Forum
- Previous Thread: Associate words with the STL map
- Next Thread: C++ Array
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





