i have problem in finishing my code

This is a discussion on i have problem in finishing my code within the C++ Programming forums, part of the General Programming Boards category; Code: this program finds the largest multiple of 5 in a randomly generated Vector of 20 elements and returns it ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    i have problem in finishing my code

    Code:
    this program finds the largest multiple of 5 in a randomly generated  Vector of 20 elements and returns it as well as its index. but What  would biggest() return if none of the integers in the vector<> are  multiple of five? and how can i correct it?
    
    
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <fstream>
    using namespace std;
    int biggest(vector<int>);
    const int k=20;
    int main()
    {
        ofstream fout("pasuxi.out");
        vector <int> v(k);
        for(int i=0; i<k; i++)
            v[i]=rand() % 71+30;
        fout<<"biggest is  "<<v[biggest(v)]
            <<".    index=  "<< biggest(v);
    }
    int biggest(vector<int> x)
    {
        int big=-1;
        int index;
        for(int i=0; i<x.size(); i++)
            if(x[i]%5==0 && x[i]>big){ 
                index=i;}
        return index;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Suggestion: learn references and/or exceptions.
    If you can't return a value that indicates "error" (which you very well could, since any number < 0 would be an invalid index, and hence could be reserved for errors), you can pass in an extra argument that would hold return information, or you can throw an exception.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help finishing a few functions.
    By Mo777 in forum C Programming
    Replies: 14
    Last Post: 03-09-2011, 06:04 PM
  2. Finishing off an assignment
    By DLH112 in forum C Programming
    Replies: 2
    Last Post: 11-11-2009, 07:13 PM
  3. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  4. Finishing up
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2002, 07:18 AM
  5. finishing up the sorting program
    By jk in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21