用C ++编写程序,找到数组中最大数字的位置。 [英] Write a program in C++ to find the position of the biggest number in an array.

查看:442
本文介绍了用C ++编写程序,找到数组中最大数字的位置。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到确切的指数值。

PS:我是一个初学者。



我尝试了什么:



im not getting the exact index value.
P.S: i'm a begginner.

What I have tried:

#include<iostream>
using namespace std;
main()
{
    int c[7],i,l,n,p;
    cout<<"Enter size of the array:"<<endl;
    cin>>n;
    cout<<"enter the elements"<<endl;

    for(i=0;i<n;i++) //i =index
    {
        cin>>c[i];
    }
    l=c[0];
    for(i=1;i<n;i++)
    {
        if (l<c[i])
        {
            l=c[i];
            break;
        }
    }
    p=0;
    p=i++;
    cout<<p<<endl;
}



放n = 5

elemntz = 6 7 8 7 9

即时通讯index = 3;(

请帮助....


on putting n=5
elemntz= 6 7 8 7 9
im getting index=3 ;(
pls help....

推荐答案

你的算法错了。当你找到一个时,你停止搜索最大值新的,但你不知道是否还有另一个。



当您检测到新的最大值时,您需要将新的最大值存储在变量中它在另一个位置。



你需要学习调试器,它会帮助你理解代码中的错误。

[更新]

Your algorithm is wrong. You stop searching the maximum as you find a new one, but you don't know if there is another one after.

As you detect a new maximum, you need to store the new max in a variable and its position in another.

You need to learn the debugger, it will help you to understand what is wrong in your code.
[Update]
引用:

你能建议我如何学习调试器吗?

can you suggest me how to learn debugger?



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。


Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


首先要记住的是发布时使用pre标签正确格式化代码。



您的算法有问题。你应该做的是保存最大项目的索引,你需要在搜索时遍历整个列表。不要突破它。此外,不要为所有内容使用单个字母的变量名称。这无助于理解代码。看来您的数据输入部分工作正常,所以这是一个可以尝试的循环:
First thing is remember to use the pre tag to format the code correctly when you post it.

Your algorithm is faulty. What you should do is save the index of the largest item and you need to traverse the entire list when searching. Don't break out of it. Also, don't use variable names of single letters for everything. That does not help to understand the code. It appears that you have the data input part working OK so here's a loop you can try :
int largest = 0;           // initialize to first item index
for( i = 1; i < n; i++ )   // start loop at second item
{
    if( c[largest] < c[i] )
        largest = i;       // save index of largest item
}

cout << "largest index is " << largest << endl;

关键是你想知道索引。您可以随时再次访问这些值,但需要跟踪比较的最大值索引并遍历整个数组。

The key point is you want to know the index. You can always access the values again when you want but you need to keep track of the index of the largest value for the comparisons and traverse the entire array.


您知道 C ++ 有一个库,不是吗?

You know C++ has a library, don't you?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
  vector <int> v;
  while (true)
  {
    int i;
    cout << "enter next integer (a letter to stop entering): " << endl;
    cin >> i;
    if ( ! cin ) break;
    v.push_back(i);
  }

  if ( v.size() > 0)
    cout << "maximum is " << *max_element(v.begin(), v.end()) << endl;
}


这篇关于用C ++编写程序,找到数组中最大数字的位置。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆