我找到了以下C程序的输出,我在GeeksforGeeks上找到了它.这是程序:
#include <stdio.h>
void fun(int ptr[])
{
int i;
unsigned int n = sizeof(ptr)/sizeof(ptr[0]);
for (i=0; i<n; i++)
printf("%d ", ptr[i]);
}
// Driver program
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
fun(arr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码的输出为" 1 2 ".但据我说,输出应该只有1.以下是我对此代码的解释:
请帮我弄清楚我错在哪里.
碰巧我正在使用Python进行编程,我正准备编写一个小石头剪刀游戏.
不幸的是,当我尝试运行我的脚本时,我收到以下错误:
file rps.py, line 53 in game
compare (move,choice)
NameError: name 'move' is not defined"
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的代码:
from random import randint
possibilities = ['rock', 'paper', 'scissors']
def CPU(list):
i = randint(0, len(list)-1)
move = list[i]
#print (str(move))
return move
def User():
choice = str(input('Your choice? (Rock [r], Paper[p], Scissors[s])'))
choice = choice.lower()
if choice == 'rock' or choice == 'r':
choice = 'rock'
elif choice == 'scissors' or choice =='s':
choice = 'scissors'
elif choice == 'paper' or choice == …
Run Code Online (Sandbox Code Playgroud) 我有一个用C语言编写的库管理系统,里面有I/O文件.dat
.如何从这个函数中获取word文件的输出:
void viewbooks(void) //show the list of book persists in library
{
int i=0,j;
system("cls");
gotoxy(1,1);
printf("*********************************Book List*****************************");
gotoxy(2,2);
printf(" CATEGORY ID BOOK NAME AUTHOR QTY PRICE RackNo ");
j=4;
fp=fopen("Bibek.dat","rb"); //the .dat file getting data to be showed
while(fread(&a,sizeof(a),1,fp)==1) // .dat file to be read
{
gotoxy(3,j);
printf("%s",a.cat);
gotoxy(16,j);
printf("%d",a.id);
gotoxy(22,j);
printf("%s",a.name);
gotoxy(36,j);
printf("%s",a.Author);
gotoxy(50,j);
printf("%d",a.quantity);
gotoxy(57,j);
printf("%.2f",a.Price);
gotoxy(69,j);
printf("%d",a.rackno);
printf("\n\n");
j++;
i=i+a.quantity;
}
gotoxy(3,25);
printf("Total Books =%d",i);
fclose(fp);
gotoxy(35,25);
returnfunc();
}
Run Code Online (Sandbox Code Playgroud) 我试图通过减少条件语句的数量来提高我的代码效率,因为我将来会在汇编中编写代码.你们有没有办法减少陈述的效率?
if (j==18)
{
store=12;
}
else if(j==30)
{
store=10;
}
else if(j==42)
{
store=8;
}
else if(j==54)
{
store=6;
}
else if(j==66)
{
store=4;
}
else if(j==78)
{
store=2;
}
else if(j==92)
{
store=2;
}
else if(j==106)
{
store=4;
}
else if(j==120)
{
store=6;
}
else if(j==134)
{
store=8;
}
else if(j==148)
{
store=10;
}
else if(j==162)
{
store=12;
}
else store=1;
Run Code Online (Sandbox Code Playgroud) 我有一个这样的课:
struct SomeClass{
SomeClass(){}
SomeClass(const SomeClass& rhs):
_m1(rhs._m1),
_m2(rhs._m2),
_m3(rhs._m3),
_m4(rhs._m4){}
SomeClass& operator=(const SomeClass& rhs){
// same as above
return *this;
}
int _m1;
std::vector<int> _m2;
std::vector<int> _m3;
int _m4;
};
Run Code Online (Sandbox Code Playgroud)
在我的程序中的某个时刻,我想保存存储在SomeClass
对象中的数据供以后使用:
SomeClass someObj = arr->getBest(); // arr is a pointer to AnotherClass,
// in which different SomeClass
// objects are initialized and then
// involved in various
// computations in AnotherClass,
// finally the best one SomeClass
// object will be save here
fwrite(&someObj, sizeof(SomeClass), 1, saveFile); …
Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么这两个变量"area"和"peri"输出0.000000?
int main()
{
double length, width, peri = (length*2) + (width*2), area = length*width;
printf("Enter the length of the rectangle in centimeters: ");
scanf("%lf", &length);
printf("\nEnter the width of the rectangle in centimeters: ");
scanf("%lf", &width);
printf("The perimeter of the rectangle is %lf cm.\nThe area of the rectangle is %lf cm.", circ, area);
return 0;
}
Run Code Online (Sandbox Code Playgroud)