c++ exercise for school

Hello guys, so I'm supposed to create a program which has to print the highest offer of a group of donators. The group of donators's information has to be managed with a structure while the group of donators should be managed with an array. Now I'm having a little trouble finding the max value of offer because I don't understand how to only use the object of a structure called float offerta, can you please guys give me any suggestions on how to do that ? because I've been looking around for any clues and haven't found anything.

the program is the following:

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;


//STRUCTURES
struct fornitori_t
{
struct indirizzo_t
{
string tipo;
string nome;
int NC;
int CAP;
string citta;
string provincia;

} ind ;


struct data_t
{
int giorno;
string mese;
int anno;
} data;

string CF;
string RS;
float offerta;
};
typedef fornitori_t forn;

//MAIN PROGRAM

int main()
{
int fornitori[2];
int max;
forn fornitors;
for (int i=0; i<fornitori[2]; i++)
{
cout<<"inserisca il suo indirizzo"<<endl;
cin>>fornitors.ind.tipo;
cin>>fornitors.ind.nome;
cin>>fornitors.ind.NC;
cin>>fornitors.ind.CAP;
cin>>fornitors.ind.citta;
cin>>fornitors.ind.provincia;
cout<<"il suo indirizzo : " << fornitors.ind.tipo << fornitors.ind.nome << fornitors.ind.NC << fornitors.ind.CAP << fornitors.ind.citta << fornitors.ind.provincia << endl;
cout<<"insersica il suo codice fiscale"<<endl;
cin>>fornitors.CF;
cout<<"il suo codice fiscale : "<< fornitors.CF << endl;
cout<<"insersica la sua ragione sociale" << endl;
cin>>fornitors.RS;
cout<<"la sua ragione sociale : "<< fornitors.RS << endl;
cout<<"inserisca data offerta"<< endl;
cin>>fornitors.data.giorno;
cin>>fornitors.data.mese;
cin>>fornitors.data.anno;
cout<<"data dell'offerta : "<< fornitors.data.giorno << fornitors.data.mese << fornitors.data.anno << endl;
cout<<"inserisca la sua offerta"<< endl;
cin>>fornitors.offerta;
cout<<"la sua offerta e : "<< fornitors.offerta;

max = fornitori[0];
// This is the for loop where I'm trying to find the max value, instead of doing a loop for all the informations of the donators I only want to use the float offerta in the fornitori_t structure //
for (int j=0; j<fornitori[2];j++)
{
if (fornitori[i]>max)
{
max = fornitori[i];
}
}
}
cout << "max is " << max;

return 0;
}




PLEASE learn to use code tags, it makes reading and commenting on your code MUCH easier.

HINT: you can edit your post and add code tags.

http://www.cplusplus.com/articles/jEywvCM9/

int fornitori[2]; Creates an array of two elements.

for (int i=0; i<fornitori[2]; i++) You access the third element in an array of 2 elements. Even if you had initialized the values before you use the array, you don't, your for loops would still be using a garbage value.

How would others do it?
http://www.cplusplus.com/reference/algorithm/max_element/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// min_element/max_element example
#include <iostream>     // std::cout
#include <algorithm>    // std::max_element

struct Foo {
  int id;
  int di;
};

int main () {
  Foo myf[] = {3,5,2,7,6,4,9,1};

  for ( auto f : myf ) {
      std::cout << f.id << '-' << f.di << '\n';
  }
  std::cout << '\n';
  
  auto maxdi = std::max_element( std::begin(myf), std::end(myf),
      [](auto lhs, auto rhs){return lhs.di < rhs.di;} );

  std::cout << maxdi->id << '-' << maxdi->di << '\n';
}
Last edited on
Unfortunately, the point in CS 101 classes is often to learn how to traverse arrays and track information yourself, so using the Standard Library for this simple task (what a normal C++ programmer should do) is out (because it is not what you should do).

To loop over an array, you need each element’s index: 0, 1, 2. Use the index to get the element value:

1
2
3
4
5
  int xs[] = { 2, 7, 3, 9, 4 };

  cout << "The first element is "  << xs[0] << "\n";
  cout << "The second element is " << xs[1] << "\n";
  …

This 0, 1, 2, … indexing scheme helps when you want to loop over the array. You just need to know how many elements are in the array. (In this case, there are 5.)

1
2
  for (int n = 0; n < 5; n++)
    cout << "The " << (n+1) << "th element is " << xs[n] << "\n";
The 1th element is 2
The 2th element is 7
The 3th element is 3
The 4th element is 9
The 5th element is 4

All of this you could have learned by simply googling something like “C/C++ arrays” and “C/C++ arrays tutorial”.


Next, to the algorithm. Given the list:

  2 7 3 9 4

How do I find the largest element? How do I prove it is the largest?

Remember, the computer cannot look at all the elements at the same time. It can, however, compare two numbers.

I can compare 2 and 7. 7 is larger.
I can compare 7 and 3. 7 is larger.
I can compare 7 and 9. 9 is larger.
I can compare 9 and 4. 9 is larger.

9 is largest.

Do you see the pattern?
You need to keep track of the current largest value.

Think on it.
Hope this helps.
Topic archived. No new replies allowed.