我正在使用SRand/Rand生成一个随机数组.数组大小取决于提示用户输入的数字.基本上,如果用户输入的大小为9,则数组应为9个数字.然后应使用带有参数的rand()填充此数组,以使数组值保持小于18.问题是,有时会生成随机大小的数组.也许每运行一次该程序的第4或第5次,该数组可能是12-14个数字.我无法看到我的代码出现问题.我在下面添加了一个片段.有人对此有所了解吗?
int main(void)
{
int N;
int i;
printf("Please enter a number\n");
scanf("%d", &N);
srand (time(NULL));
int numarray[N];
for(i=1; i<numarray[N]; i++)
{
numarray[i]=rand()%21;
printf("%d\n", numarray[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 是否可以在 C++ 中进行 while 循环,直到小数达到一定精度?我想对 arctan 和 pi 进行长时间的计算,并且我希望循环运行,直到使用公式 pi=4(arctan (1.0)) 将 pi 计算到小数点后第 10 位。我正在使用泰勒级数公式手动计算反正切。我知道这些计算有内置函数,但这是一项家庭作业,所以我必须这样做。我不是在寻找问题的解决方案,只是寻找使用精度的循环是否可行。谢谢!
编辑:
我仍然坚持这个!即使在仔细阅读了每个人的提示之后,我也无法为 while 循环提出正确的论点。真的需要帮助。这是我想出的代码:
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
int i;
long double result;
long double pi;
int y=3;
int loopcount=0;
long double precision;
cout<<"Start\n";
result=1-(pow(1,y)/y);
do
{
y=y+2;
result=result+(pow(1,y)/y);
y=y+2;
result=result-(pow(1,y)/y);
pi=4*(result);
precision=(pi*(pow(10,11))/10);
loopcount++;
}
while(//This is the problem!);
cout<<"Final Arctan is:"<<endl;
cout<<setprecision(20)<<result<<endl;
cout<<"Final Pi is:"<<endl;
cout<<setprecision(9)<<pi1<<endl;
cout<<"Times looped:"<<endl;
cout<<loopcount<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我程序中的一个功能.使用cout语句,程序编译并运行.如果我删除cout语句,我会返回一个分段错误.我在linux mint上使用G ++编译器.有没有人对此有任何想法?
int findFactors(int n,int factors[],int numFactors)
{
int m=n;
int k=2;
int i;
while(m!=1)
{
for(k=2;k<=m;k++)
{
if(m%k==0)
{
factors[i]=k;
cout<<"Prime Factor: "<<factors[i]<<endl;//This is the offending statement!
factors[i++];
numFactors++;
break;
}
}
m=(m/k);
}
return numFactors;
}
Run Code Online (Sandbox Code Playgroud) 关于Oracle SQL中的连接表的快速问题.我理解他们的功能和他们在"多对多"关系中的角色,但是"一对多"关系呢?我有两张桌子,Employees和Positions.每个员工只能持有一个职位,但每个职位可以有很多员工.例如,John Doe只能是销售主管,但公司有4名销售主管.这是我到目前为止编码的方式:
CREATE TABLE Positions (
position_id NUMBER(2) NOT NULL,
position_name VARCHAR2(25) NOT NULL,
CONSTRAINT pk_position PRIMARY KEY(position_id)
);
CREATE TABLE Employee (
emp_id NUMBER(3) NOT NULL,
emp_name VARCHAR2(30) NOT NULL,
emp_position NUMBER(2) NOT NULL,
emp_salary NUMBER(5) NOT NULL,
CONSTRAINT pk_employee PRIMARY KEY(emp_id),
CONSTRAINT fk_emp_pos FOREIGN KEY (emp_position)
REFERENCES Position(position_id)
);
CREATE TABLE pos_emp (
position_id NUMBER(2) NOT NULL,
emp_id NUMBER(3) NOT NULL,
CONSTRAINT pk_pos_emp PRIMARY KEY(position_id, emp_id)
);
Run Code Online (Sandbox Code Playgroud)
它是否正确?是否需要:
a.Employee表中的外键?
湾 联结表? …
我正在编写一个程序来读取文本文件的前20行.当读取前20行时,用户可以选择继续读取接下来的20行或退出程序.然而,我发生的是打印20行,用户提示出现,然后自动打印接下来的20行,而不等待用户的输入.之后,它将打印用户提示,然后等待输入.我知道这是一个简单的问题,但我没有看到它!到目前为止,根据对我的问题的回复,我对代码进行了一些修改:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char fname[20];
char c, input;
int line;
line = 0;
printf("Please enter the name of the file you wish to view\n");
scanf("%s", fname);
fp = fopen(fname, "r");
if (fp == NULL)
{
printf("The file did not open correctly");
exit(0);
}
else
{
printf("The file opened correctly\n");
}
while(c != EOF && input != 'q')
{
c = getc(fp);
printf("%c", c);
if (c == '\n')
{
line++;
while (line==20) …Run Code Online (Sandbox Code Playgroud) 有人可以告诉我为什么这段代码不起作用?它确实编译.当我在命令行中输入decrypt作为argv [1]参数时,它仍然给我else输出.即argv [1]即使它应该是不满意的.这是一项正在进行的工作,因此请忽略其他代码
if ((argv[1] == "decrypt"))
{
printf("Decrypting...\n");
c = getc(fp1);
if (c != EOF)
{
fread(inputbuffer, sizeof(char), 50 , fp1);
printf("%s", inputbuffer);
/*while(inputbuffer[i]!=EOF)
{
fputc((inputbuffer[i] / 2) - 5, fp2);
}*/
}
}
else {printf("argv not working");}
Run Code Online (Sandbox Code Playgroud) 我正在研究一个程序但是在涉及到类时无法理解使用指针.我知道我必须为指针使用分配内存,new并且在不使用类时可以使用它.我找不到一个简单的教程来解释如何完成这个特定的任务.有人可以给我一些帮助吗?这是我到目前为止所做的相关片段,但它输出的是随机字符:
"Hangman.c"
{
class Hangman
{
public:
...
char* remainingLetters();
Hangman()
{
char* remaining=new char[26];
}
~Hangman();
private:
char* remaining;
}
"Hangman.cpp"
{
...
char* Hangman::remainingLetters()
{
...does task to find remaining letters;
return remaining;
}
ostream& operator<< (ostream &out, Hangman &game)
{
out << "Letters remaining are: " << game.remaining <<endl
return out;
}
}
"main.cpp"
{
...
cout << game;
...
}
Run Code Online (Sandbox Code Playgroud) c++ constructor memory-management class operator-overloading
我正在遍历列表并将每个元素与2个字典进行比较.列表元素是字典中的键.一些元素在2个字典中,一些在一个中,一些在没有.
for i in range(0,len(mylist)-1):
if mylist[i] == mydict[mylist[i]]:
print 'in dict 1'
elif mylist[i] == mydict2[mylist[i]]:
print 'in dict 2'
else: print 'not in dictionaries'
Run Code Online (Sandbox Code Playgroud)
我的问题是它没有超越第一个elif声明.如果它在2个词典中找不到列表元素,则会输出键错误.我无法理解它,因为我在代码的另一部分中有另一个循环,它与此非常相似并且工作正常.如果某个键不在字典中,我希望else打印该语句.不是关键错误
可能重复:
在onCreate之前,活动无法使用系统服务?
我试图将数据库的值返回到listview,但我收到一个错误.我使用游标从表中获取数据,使用SimpleCursorAdapter读取游标.这是执行此操作的方法:
public void getData(){
String[] columns = new String[] {ModuleDB.KEY_ROWID,
ModuleDB.KEY_MODCODE,
ModuleDB.KEY_MODNAME,
ModuleDB.KEY_LECPRAC,
ModuleDB.KEY_DAYS,
ModuleDB.KEY_TIMESTART,
ModuleDB.KEY_TIMEEND,
ModuleDB.KEY_LOCATION,
ModuleDB.KEY_INFO};
int[] to = new int[]{R.id.modcode_entry,
R.id.modname_entry,
R.id.modlecprac_entry,
R.id.modday_entry,
R.id.modtimestart_entry,
R.id.modtimeend_entry,
R.id.modlocation_entry,
R.id.modaddinfo_entry};
Cursor curs = myDatabase.query(ModuleDB.DB_TABLE, columns, null, null, null, null, null);
if (curs != null)
curs.moveToFirst();
SimpleCursorAdapter dataSource = new SimpleCursorAdapter(ModuleDataSource.this, R.layout.list_entry, curs, columns, to);
this.setListAdapter(dataSource);
// Make sure to close the cursor
curs.close();
}
Run Code Online (Sandbox Code Playgroud)
此方法导致异常: "System Services not available to activites before onCreate()"
谁能帮我这个?我找不到任何关于游标和cursoradapter的好教程,我真的完全迷失了
编辑:
这可能有所帮助.
MainActivity.java
public …Run Code Online (Sandbox Code Playgroud)