Python家庭作业分配要求我写一个函数"将输入作为正整数,并打印出一个乘法,表格显示所有整数乘法,包括输入数字."(也使用while循环)
# This is an example of the output of the function
print_multiplication_table(3)
>>> 1 * 1 = 1
>>> 1 * 2 = 2
>>> 1 * 3 = 3
>>> 2 * 1 = 2
>>> 2 * 2 = 4
>>> 2 * 3 = 6
>>> 3 * 1 = 3
>>> 3 * 2 = 6
>>> 3 * 3 = 9
Run Code Online (Sandbox Code Playgroud)
我知道如何开始,但不知道接下来该做什么.我只需要一些算法帮助.请不要写正确的代码,因为我想学习.而是告诉我逻辑和推理.
这是我的推理:
所以这就是我到目前为止所做的:
def print_multiplication_table(n): …Run Code Online (Sandbox Code Playgroud) 我有以下功能:
int conMS(time_in_seconds) {
int minutes, seconds;
minutes = time_in_seconds / 60;
seconds = time_in_seconds % 60;
return minutes, seconds;
}
Run Code Online (Sandbox Code Playgroud)
当在另一个函数中使用时,我收到几条错误消息:
warning: left-hand operand of comma expression has no effect [-Wunused-value]
warning: left-hand operand of comma expression has no effect [-Wunused-value]
minutes,seconds = conMS(time);
warning: ‘minutes’ is used uninitialized in this function [-Wuninitialized]
warning: left-hand operand of comma expression has no effect [-Wunused-value]
return minutes, seconds;
Run Code Online (Sandbox Code Playgroud)
无论如何,有没有办法可以从函数返回两个值.这两个值可以是任何东西:a int和a char,a float和a int......
我很抱歉,如果这对你来说很简单,但我是C的初学者,我能学到的唯一方法就是提问.另外,请尽量简化您的解释.
更新:这可以通过指针轻松完成,如下所示.
void …Run Code Online (Sandbox Code Playgroud) 我有一个清单:
my_list = [12,8,0,4,7,21,27,"O",29,3,"X","O","X","X"]
Run Code Online (Sandbox Code Playgroud)
该列表在整个计划中得到更新.每次更新时,我都要查看它是仅包含数字还是仅包含字符串,或两者都包含.我可以用它做什么?
我需要检查列表中的所有元素.有什么建议吗?**
**有关代码如何工作的说明请.
谢谢大家的帮助.感谢您的帮助.
我有:
#include <stdio.h>
int main()
{
char ch[] = "Hello";
char wd[] = "World";
char ex[] = "!";
printf("The size of a char: %ld\n",sizeof(char));
printf("The size of ch[]: %ld\n",sizeof(ch));
printf("The size of ex[]: %ld\n",sizeof(ex));
printf("The size of wd[]: %ld\n",sizeof(wd));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产量:
The size of a char: 1
The size of ch[]: 6
The size of ex[]: 2
The size of wd[]: 6
Run Code Online (Sandbox Code Playgroud)
我的问题:由于char的大小是1个字节,为什么ch []的大小不是5个字节?因为它有5个字符(H,e,l,l和o)
与wd []和ex []相同.这里发生了什么?
对不起,如果这是一个很容易的,但我是C的新手.
我正在玩C,并决定建立我自己的功能strlen()而且我{}做了.但通过这样做,我很震惊地找到了可以做的事情.这是我的代码:
#include <stdio.h>
#include <assert.h>
int mystrlen(char *s)
{
int i = 0;
assert(s[0] !='\0');
for(i = 0; s[i] !='\0'; i++)
// Placing {} here causes the error to disappear //
return i;
}
int main(void)
{
char hello[] = "hello";
int len = mystrlen(hello);
printf("Length = %d\n", len);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行此代码会产生此错误:
mystrlen.c:12:1: warning: control reaches end of non-void function [-Wreturn-type]
Run Code Online (Sandbox Code Playgroud)
但在 {} while循环之后放置后不再有错误.任何人都可以向我解释这种行为吗?
我试图重载>>运算符,以便当用户输入分数时,cin会将分数存储到类型为Fraction的Object中.
摘自头文件:
// Calculator.h
friend istream &operator>>( istream &, Fraction &);
private:
signed int numo; // fraction numerator
signed int deno; // fraction denomenator
char ch; // the '/' character
Run Code Online (Sandbox Code Playgroud)
摘自实施文件:
//Calculator.cpp
// insertion operator overloading
istream &operator>>( istream &input, Fraction fraction)
{
input >> fraction.numo;
input >> fraction.ch;
input >>fraction.deno;
return input;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译Calculator.cpp并在另一个文件中编译驱动程序函数时,我遇到了很多错误.为什么这不起作用?请彻底解释你的答案,我想学习.
更新:
**错误:变量,numo deno和ch是'私有'