我写了一个短倍频脚本绘制功能sum(sin(2k-1)/(2k-1))的k = 1..n条款.(我试图模拟连续项如何使输出收敛到方波.
% Program to model square-wave using sum of sines
terms=3
theta=linspace(0, 6*pi, 1000);
k=[1:terms]';
n=2*k-1;
q=sin(n*theta)./n;
y=sum(q);
plot(theta, y);
Run Code Online (Sandbox Code Playgroud)
sum()对于术语> 1,它工作正常(即函数返回包含每列总和的向量)但是当术语== 1(即它应该只绘制一个正弦波)时,该sum()函数计算行的总和并返回一个标量.
sum()即使只有一行,我如何得到函数总计每列,或者如何重新整形或切片或任何行向量,使得它不是有效成为二维矩阵的维度n的一维向量尺寸1xn?
为什么Hi要打印4次.它将在函数最终返回完整值之前执行一次.
#include <stdio.h>
int k = 0;
int factorial (unsigned int i) {
if (i <= 1) {
return 1;
} else {
k = i * factorial(i - 1);
}
printf("hi"); // Why is Hi being printed 4 times.
return k;
}
int main() {
int i = 5;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 strstr() 在字符串中查找子字符串。仅使用 char[] 有效,char* 无效,导致分段错误。
所以这个正在工作:
int main() {
char str[] = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这一个不起作用:
int main() {
char *str = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个既不是:
int main() {
char *str = "apple apple peach";
char *peach = "peach";
char *p;
p = strstr(str, peach); …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
#include<string.h>
typedef struct
{
float TotalSize;
float AvailableSize;
}SdMemoryInfo;
void SdGetMemoryUsageRespCsccToGui(SdMemoryInfo *abcdSdMemoryInfo)
{
printf("\nSize of(SdMemoryInfo): %d %d",sizeof(abcdSdMemoryInfo),sizeof(float)); //Size of abcdSdMemoryInfo
}
int main()
{
SdGetMemoryUsageResp mSdGetMemoryUsageResp;
SdMemoryInfo vSdMemoryInfo[2];
vSdMemoryInfo[0].TotalSize = 1;
vSdMemoryInfo[0].AvailableSize = 2;
vSdMemoryInfo[1].TotalSize = 3;
vSdMemoryInfo[1].AvailableSize = 4;
printf("\nSize of(SdMemoryInfo): %d %d",sizeof(vSdMemoryInfo),sizeof(float)); //Size of vSdMemoryInfo
SdGetMemoryUsageRespCsccToGui(vSdMemoryInfo);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Size of(SdMemoryInfo): 16 4
Size of(SdMemoryInfo): 8 4
Run Code Online (Sandbox Code Playgroud)
为什么是结构数组的大小vSdMemoryInfo和abcdSdMemoryInfo不同?
我的代码:
vector<int> v[10];
const int x = 3;
void clearBySimpleLoop(){
for (int i = 0; i < x; i++){
v[i].clear();
}
}
int main()
{
for (int i = 0; i < x; i++){
v[i].push_back(11+i);
v[i].push_back(11+i+1);
v[i].push_back(11+i+2);
}
for (auto vec : v) vec.clear(); //#01. Contents are not cleared. Size of the contained vectors remains same.
clearBySimpleLoop(); //#02. Contents are cleared. Size of the contained vectors becomes zero.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是为什么foreach循环(#01)中的代码无法清除数组中的向量,而简单的for循环(#02)成功了?
说说Arrays中定义的这个方法:
public static int binarySearch(int[] a, int key)
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么返回(-(insertion point) - 1)而不是-(insertion point)在数组中找不到匹配项的情况。
https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch(int[],%20int)
这可能是Math.abs((-(insertion point) - 1))等于数组大小的原因吗?
如果你想知道我为什么要问这个问题,我看到为了找到插入点,我基本上必须做减法。
int returnedVal = Arrays.binarySearch(arr, needle);
if (returnedVal < 0)
insertionPoint = Math.abs(returnedVal) - 1;
Run Code Online (Sandbox Code Playgroud) 在什么基础上,类对象的大小显示为12?
class testvector
{
public : vector<int> test;
};
int main()
{
testvector otestvector;
cout<<"size :"<<sizeof(otestvector)<<"\n";
cout<<"size of int :"<<sizeof(int);
}
Run Code Online (Sandbox Code Playgroud)
输出:
size :12
size of int :4
Run Code Online (Sandbox Code Playgroud) 我是初学者,我必须打印 # 中的字母“N”。到目前为止,我只能打印 |\ ,所以我仍然缺少最后一条“腿”。我实际上不知道我是如何做到这一点的..如果有人可以帮助我或解释一下!这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 9; i++)
{
cout << "#";
for (j = 1; j <= 12; j++)
{
if (i == j)
{
cout << "#";
}
else
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)