所有连续子阵列的最大总和的程序 [英] Program for largest sum of all contiguous subarrays

查看:83
本文介绍了所有连续子阵列的最大总和的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//program for Largest sum of all contiguous subarrays
#include<stdio.h>
#include<math.h>
int main()
{
    int n,p,i,j,t,k=0,max;
    int a[n],r[100];
    printf("enter the size of the array \n");
    scanf("%d",&n);
    //q=pow(2,n);

for(p=n;p>0;p--)
    {
        for(i=0;i<p;i++)>
        {
            t=0;
            for(j=0;j<=n-p;j++)
            {
                t=t+a[j];
                r[k]=t;
                k++;
            }
        }
    }
    max=r[0];
    for(i=1;i<k;i++)>
    {
        if(max<r[i])>
        {
            max=r[i];
        }
    }
    printf("%d",max);
return 0;
}





我的尝试:



为什么它说.............



What I have tried:

why it is saying.............

Program:(.text+0x93): undefined reference to `pow' collect2: error: ld returned 1



退出状态.........请解释



这是编译器显示的错误


exit status .........PLEASE EXPLAIN

this is the error shown by the compiler

推荐答案

ld 是链接器。它告诉你它在你的编译代码和所有包含的库中都找不到 pow 函数。 pow 函数与 libm 库的所有其他数学函数部分类似。因此,您必须使用链接器的 -lm 选项将您的应用程序链接到该库(另请参阅man 3 pow)。使用GCC进行编译时,将选项添加到 gcc 命令行。



示例:

ld is the linker. It tells you that it can not find the pow function in your compiled code and all included libraries. The pow function is like all other math function part of the libm library. So you have to link your application with that library using the -lm option for the linker (see also "man 3 pow"). When using GCC for compilation, add the option to the gcc command line.

Examples:
 # Compile and link single C file with libm
gcc -Wall -lm <your_source>.c

 # Compile and link single C++ file with libm
g++ -Wall -lm <your_source>.cpp

 # Using separate compile and link steps
gcc -Wall -c <your_source>.c
gcc -lm -o <app_name> <your_source>.o


嗨。



好​​像你是C ++的新手。程序中有很多错误。



1.#include之后应该是一个头文件。

2.你不能write int a [n],因为在编译期间编译器不知道n的值,因此它不能为at堆栈分配大小。您应该使用新删除,如下所示。在使用以下代码之前,请记住读取n的值。

//读取值n

int * a = new int [n];

//使用* a



//使用后删除

delete [] a;

3.在if(max< r [i])>之类的行中,什么是>干嘛?



- 拉杰夫
Hi.

Seems like you are new to C++. There are many errors in the program.

1. "#include" should be followed by a header file.
2. You cannot write int a[n], as during compilation the compiler doesn't knows the value of n, and therefore it cannot allocate size for a at stack. You should use new delete as displayed below. Remember to read the value of n before using the following codes.
//read value of n
int *a = new int[n];
//use *a

//delete after using
delete []a;
3. In lines like "if(max<r[i])>", what is ">" doing?

--Rajeev


这篇关于所有连续子阵列的最大总和的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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