我有以下函数用于生成对称矩阵:
void genMatrix(int n, double A[n][n])
{
int i,j;
int count=0;
for (i=0; i<n; i++)
for (j=i+1; j<n; j++)
{
count++;
A[i][j]=count;
A[j][i]=count;
}
}
Run Code Online (Sandbox Code Playgroud)
当我用这些参数调用函数时:
int n = 10000;
double A[n][n];
genMatrix(n,A);
Run Code Online (Sandbox Code Playgroud)
它给了我一个分段错误,我不明白为什么.我也尝试只用1个值填充矩阵,但它不会改变任何东西.可能是什么问题呢?使用较小的n值,如1000,它工作正常.
假设向量数组按以下规则排序:- A[i] > A[j] 对于 A[i] (a,b) 中的所有对和 A[j] a>c 中的所有对 (c,d) 和b>d。假设输入数组已排序。现在给定一个上述类型的数组,
A[0] = (0,1)
A[1] = (4,3), (2,5)
A[2] = (12,4), (10, 6)
...
Run Code Online (Sandbox Code Playgroud)
现在您将一对作为输入,如何使用内置的lower_bound 函数找到lower_bound。我写了一个代码,但它给了我一些错误。我错过了什么?
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int,int> mypair;
vector <mypair> A[100008];
mypair B;
bool operator < (const mypair &a1, const mypair &a2){
return (a1.first < a2.first && a1.second < a2.second);
}
bool operator < (const vector<mypair> &a1, const mypair &a2){
for(int i = 0; i< a1.size();i++){
if (a1[i] < …Run Code Online (Sandbox Code Playgroud) 我是C的新手所以我们有这个任务,我试图让它更复杂.
我目前的问题是制作char*类型的wordMin,我不知道为什么它还没有.
我尝试制作一个类似但更短的程序
int numHrs = 2;
char* hours;
if (numHrs == 1) {
hours = "hour";
} else {
hours = "hours";
}
printf("\n%s\n\n", hours);
Run Code Online (Sandbox Code Playgroud)
在这里它工作得很好.
任何有关我的pastebin代码的反馈都非常受欢迎,因为我不熟悉编码并且不知道最聪明的做事方式.
#include <stdio.h>
int singleFib(int x,int a,int b);
int multiFib(int x);
void main(){
int n,i;
printf("How much?\n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("%5d. number: %d - %d \n",i+1,multiFib(i),singleFib(i,1,1));
}
getch();
return 0;
}
int multiFib(int x){
if (x<2){
return 1;
}
else{
return multiFib(x-2)+multiFib(x-1);
}
int singleFib(int x,int a,int b){
if (x<2){
return 1;
}
else{
return singleFib( x-1, b,(a+b));
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误在于
singleFib(i,1,1) in `printf`
Run Code Online (Sandbox Code Playgroud)
为什么这个问题?我该如何解决这个问题呢?我正在使用代码块
Codeblocks\Fiberonacci\main.c | 14 |对`singleFib'|的未定义引用 错误是我可以解决的吗?
我正在为一个真正的应用项目做C++程序.
我用valgrind做内存泄漏检查.
我有 :
10个块中的160个字节肯定在丢失记录1中丢失2 == 14312 ==在0x4A0846F:malloc(vg_replace_malloc.c:236)
但是,我已经调用了free()来释放它.
怎么解决?为什么我有内存泄漏?
任何帮助将不胜感激.!
代码如下:
#include <stdio.h>
#include <stdlib.h>
int * GetSomeIDs() ;
/*-------- GetSomeIDs-----*/
typedef struct {
char* alias; /* '\0'-terminated C string */
int specific_id;
} aliasID;
int GetNumberOfAliases(void)
{
return 10;
}
aliasID *GetNextAlias(void)
{
aliasID *p = (aliasID *) malloc(sizeof(aliasID)) ;
p->alias = "test alias";
p->specific_id = rand();
return p;
}
int *GetSomeIDs2(aliasID ***a, int *aIDsize , int *idSize)
{
int *ids = (int *) malloc(sizeof(int) * 8) ; …Run Code Online (Sandbox Code Playgroud) 如果我有一个类定义
class myClass
{
void x();
};
void myClass::x()
{
hello(); // error: ‘hello’ was not declared in this scope
}
void hello()
{
cout << "Hello\n" << endl;
}
Run Code Online (Sandbox Code Playgroud)
如何调用在类范围之外定义并位于同一文件中的函数?我知道我可以使用,Namespace::function但我不确定在这种情况下我应该使用什么Namespace
我似乎无法在C下面生成随机数Ubuntu 12.04.
我写了以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
int main (int argc,char* argv[])
{
int number;
clear();
number = rand() % 2; // want to get only 0 or 1
printf("%d",number);
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我将文件命名为"test_gcc.c".
之后我编译它:
$ sudo gcc -o test_gcc test_gcc.c
Run Code Online (Sandbox Code Playgroud)
我收到以下消息:
/tmp/ccT0s12v.o: In function `main':
test_gcc.c:(.text+0xa): undefined reference to `stdscr'
test_gcc.c:(.text+0x12): undefined reference to `wclear'
test_gcc.c:(.text+0x44): undefined reference to `stdscr'
test_gcc.c:(.text+0x4c): undefined reference to `wgetch'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
有人能告诉我,我做错了什么吗? …
我写的时候:
if ((1/3) > 0) ...
Run Code Online (Sandbox Code Playgroud)
我是否需要将其中一个操作数转换为(float)以使此条件成立?或者什么是默认的变量类型C正在使用?
如果我愿意写:
if ((1.0/3) > 0) ...
Run Code Online (Sandbox Code Playgroud)
情况现在已经改变了,因为编译器注意到其中一个操作数必须是浮点数?
请检查以下程序
#include <stdio.h>
#include<stdlib.h>
int main()
{
char *Date= NULL;
unsigned short y=2013;
Date = malloc(3);
sprintf((char *)Date,"%hu",y);
printf("%d %d %d %d %d \n",Date[0],Date[1],Date[2],Date[3],Date[4]);
printf("%s %d %d",Date,strlen(Date),sizeof(y));
}
output:
50 48 49 51 0
2013 4 2
Run Code Online (Sandbox Code Playgroud)
我如何得到字符串长度4而不是2,因为我将一个短整数值放入内存,所以它应该占用2字节的内存.但它是如何占用4字节.
每个字节如何从输入获得2 0 1 3,而在一个字节中为20,在另一个字节中为13.
我想把20到1个字节和13个放到另一个字节.怎么做.请告诉我
请给出一些答案.
我正在尝试找到如何用橙色为文本着色.
我试过这个:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | BACKGROUND_GREEN);
Run Code Online (Sandbox Code Playgroud)
但它会给我一个红色的绿色文字.
是否有可能将颜色变为橙色?我尝试了在互联网上找到的不同代码,但没有一个给我橙色.
谢谢您的帮助.