我试图输入一个数组,然后使用for循环按反向顺序打印它。首先,我试图按原始顺序打印数组元素如下:
#include <stdio.h>
int main()
{
int a[100];
int n,i;
int *p=a;
printf("Enter size of array:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++){
printf("Ent
我有一个关于冒泡排序伪代码中特定行的问题。
此伪代码取自
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do //THIS IS THE LINE I DON'T UNDERSTAND
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swa
目前,我收到以下代码的错误。错误是索引4超出了长度4的范围。基本上,我要做的是返回数据没有按升序排序的第一个索引。如果对数据进行了排序,则函数将返回数组的长度。例如,{10,20,90,5,70}将返回3 (90 > 5)。如果数据无效,则返回-1。
public static int Sorted(int[] data) {
if (data == null)
return -1;
for (int i = 0; i < data.length; i++) {
if (data[i + 1] < data[i])
我有一个循环,它将填充一个数组,但是当我运行代码时,它似乎没有填充元素。该代码用于计算球体中同心壳的体积。我已经计算出了半径网格,其中元素的数量设置为壳+1,以说明中心点。现在我想在这个volume方法中使用它,它将返回与所选择的相同数量的shell。如果我将体积网格初始化为shell + 1,我可以获得值的唯一方法。我返回shell的数量,不包括额外的元素。这很好,但是,我需要使用这个网格进行其他计算。这个额外的、未定义的元素会影响其他计算吗?
public class VolGrid {
public double vol[];
double p = Physi
我正在编写一个程序,在这个程序中,我需要在一个整数数组中获取元素的索引,以便索引右边的所有元素都大于从0到该索引位置的所有元素。
例如:
Case : 1给定的输入- { 5, -2, 3, 8, 6 }则需要索引位置为2 (i.e array element with value 3),因为索引2之后的所有元素都大于从索引0到索引2的所有元素,即{5,-2,3}
Case : 2给定的输入- { -5, 3, -2, 8, 6 }需要索引位置为2 (i.e array element with value -2),因为索引2之后的所有元素都大于从索引0到索引2的所有元素,即{-5,3,-2}
下面的代码让用户输入第一个数组的长度,然后获取第一个数组的值。在此之后,代码接受第二个数组的值,该数组的大小比初始数组小一个。在找到这些值之后,代码会对两个数组进行置换。然而,第二个数组总是不能正确排列,谁能告诉我为什么?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define N 12
#define M 11
int print(int *arr, int n)
{
int i;
for ( i = 0 ; i < n ; i++)
printf(&
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
struct Person{
char *name;
char sex;
int age;
struct Person *ancestor;
int n;
};
void p_person(struct Person *this);
struct Person *stack_init()
{
struct Person *this=
所以我的要求是
要求:n >= 1.元素a.安-1存在。
承诺
如果n为== 1,则返回值为1。
如果n>1时,则返回值为1。an-1构成一个算术序列.
否则,返回值为0。
到目前为止我的职责是
int is_arith_seq(const int *a, int n)
{
assert(n >= 1);
if (n == 1)
return 1;
int i;
int initaldif = a[1]-a[0];
int currentdif,result;
for
我试图制作一个程序,将数组的元素从右向左移动,然后打印它们,但是程序打印的唯一东西是最后一个值:为什么?
int main (){
int n = 5;
int array[n];
int i = 0;
int temp = 0;
while (i < n) {
printf("Insert the values\n");
scanf("%d", &array[i]);
i++;
}
i = 0;
temp = array[i];
我试图解决一个问题,这个问题要求返回somePredicate函数返回false的数组中第一个元素的下标。如果没有这样的元素,则返回-1。
注意,我需要使用somePredicate来计算数组中每个元素的正确性,而且我的firstFalse函数中不能有任何循环。
我的功能是这样的
int firstFalse(const string a[], int n)
其中a[]是字符串数组,n是数组中的元素数。
这是somePredicate函数
bool somePredicate(string s)
{
int nDigits = 0;
for (int k = 0; k !=
我有一些序列号:
double* a = malloc((1000000) * sizeof(double));
double* b = malloc((1000000) * sizeof(double));
double totalA = 0;
for (int i = 0; i < 1000000; i++) {
if (i == 0) {
a[i] = sin(i);
}
b[i] = sin(i+1);
if (i < 1000000-1) {
a[i+1] = b[i];
}
tot
我正在学习递归,我们应该从数组中得到最大的数字,但我不明白解决方案。
#include<stdio.h>
#include<stdlib.h>
int biggestNumber(int *array, int n);
int main(void){
int n=3;
int array[3]={3,4,1};
fprintf(stdout, "|||||%d\n", biggestNumber(array,n));
return 0;
}
int biggestNumber(int *array, int n){
if(n==1
我正在做一项任务,它要求我创建一个shell。我需要在自己的进程中运行大多数命令。然而,当我需要实现所有子进程之间的管道时,就会出现困难。
据我所知,到目前为止,我应该在父进程中创建所有管道,然后将管道复制到STDIN/STDOUT。因此,我创建了一个函数,为每个命令创建一个新管道,如下所示:
int count = 2 * amountOfCommands
int fd[count];
for (int i = 0; i < count; i++) {
pipe(fd); //I have error checking, but I left it out here.
}
让我
我是一个新的学习数据结构和算法的人,我需要帮助来解决这个问题。
具有N个元素的数组的最佳值定义为数组所有元素中最佳元素的之和。元素Ai的最佳定义如下所示
a: The best of element A[i] is 1 if, A[i-1]<A[i]<A[i+1]
b: The best of element A[i] is 2 if, A[i]> A[j] for j ranging from 0 to n-1
and A[i]<A[h] for h ranging from i+1 to N-1
编写
给出了一个简单的pseoducode算法:
for j=1 to A.length-1 //first line
for i =1 to A.length-j //second line
if A[i-1] >A[i]
swap A[i-1] and A[i]
我听说第二行是这样的(最坏的情况是:
n+(n-1)+...+2 = n(n+1)/2-1
我知道,当第一行运行时,第二个循环运行n次,每次j的下一次迭代,第二个循环运行1次(n-1) +(n-2)等等。我知道这显然是一个求和,但我不明白为什么添加的最后一个东西是2 (对于第二行)。
如有任何意见,将不
基本上,我试图将一个巨大的一维向量分成给定大小的块,这些块通过函数传递。这个函数应该返回一个2D向量,然后我就可以访问不同的块了。我已经找到了一个合适的算法来做这件事,然而,它是在Matlab中,我不知道如何将元素放置在2D向量中。
MatLab代码:
function f = block(v, N, M)
n = length(v);
maxblockstart = n - N + 1;
lastblockstart = maxblockstart - mod(maxblockstart-1 , M);
numblocks = (lastblockstart-1)/M + 1;
f =
我正在尝试确定一个算法的运行时复杂度,该算法采用一个数组,并确定数组中最长的连续子序列的长度,其中子序列的最大值和最小值的差小于或等于某个给定值。
代码
input = [16,19,20,22,27,23]
compareValue = 3
tmp,length = 0,0
for i in range(0,n):
newArray = []
for j in range(i,n):
newArray.append(input[j])
if abs(max(newArray) - min(newArray)) <= compareValue:
const int N = 5;
int person[] = {2, 3, 12, 5, 19};
int big = 0;
int small = 100;
int i;
for (i = 0; i <= N; i++)
{
cout << "Person[" << i << "] ate: " << person[i] << endl;
//cin >> person[i];
for (int j = i+1; j <= N; j++)
以下算法的大O值是多少?为什么是这个值?
algorithm A (val array <ptr to int>)
1 n = 0
2 loop ( n < array size )
1 min = n;
2 m = n;
3 loop ( m < array size)
1 if (array[m] < array[min])
1 min = m;
4 swap(array[min],array[n]);
3 n = n + 1
我的回答是O(n^2),我说的对
所以我必须用structs和scanf来编程一个标量积((x1*y1)+(x2*y2))。然而,我的程序只是跳过y1,并将y1和x2计算为相同的数字,即使我输入了完全不同的数字??我做了以下工作:
struct _point2d
{
double x[1]; // this means x1 is x[0] and x2 is x[1]
double y[1];
};
double PscalarProduct( double a, double b, double c, double d )
{
printf("The sca
我想构造一个双射函数f(k, n, seed)从[1,n]到[1,n],其中1<=k<=n和1<=f(k, n, seed)<=n对每个给定的seed和n。函数实际上应该从1,2,...,n的随机排列返回一个值。随机性由seed决定。不同的seed可能对应于不同的排列。我希望f(k, n, seed)函数的时间复杂度对于每个1<=k<=n和任何给定的seed都是O(1)。
谁知道我怎么能构造这样的函数呢?随机性允许为伪随机性.n可以非常大(例如,>= 1e8)。
我正在编写一个代码来输入主题信息,在其中我把void函数和数组作为一个对象。但我不确定什么时候我想循环,它要到最后才会来。看看密码。
void calculateCGPA::getGPA() {
cout << "Enter the the name of the subject: ";
cin >> subjectName;
cout << "Enter the credit hour:";
cin >> credithour;
cout << "Ent