int main()
{
int numbers[30];
int i;
// making array of 30 random numbers under 100
for(i=0;i<30;i++)
{
numbers[i] = rand() % 100;
}
// finding the greatest number in the array
int greatest = 0;
srand(1);
for(i=0;i<30;i++)
{
if ( numbers[i] > greatest)
greatest = numbers[i];
}
Run Code Online (Sandbox Code Playgroud)
那我怎么告诉程序显示数组的最大值?谢谢
我正在赶上指针.我写了几行代码来测试我可以动态分配2d数组的不同方法(在底部发布)
我的问题如下:
非常感谢,马特
#include <stdio.h>
#include <stdlib.h>
struct myObject
{
int data;
myObject(int i)
{
data = i;
}
myObject()
{
data = 0;
}
};
int main(){
int r = 7;
int c = 6;
printf("Objects using NEW===============================\n");
//notice the triple pointer being assigned a new double pointer array
myObject*** objectNew = new myObject** [r];
for(int i=0;i<r;i++)
{
//objectNew is a 1D array of double pointers, however if we assign another layer of pointers, it …Run Code Online (Sandbox Code Playgroud) 我想要do while循环来检查输入的输入是否为R OR P.我认为它正在检查两者,当我运行时它到达那个部分暂停一分钟然后我得到"Cpu Limit Exceeded(在另一个相关的说明中,我是否有破坏某些东西的危险?
/************************************************/
/* Name: servcode */
/* Description: Get service type */
/* Parameters: N/A */
/* Return Value: servcode */
/************************************************/
char servcode()
{
char servcode = 'a'; //Define var for service code
char serviceyn = 'n'; //Define var for user verify
int i = 1; //Define var for sentinel loop
do {
cout << "\n" << "\n" << "Please enter your service code, [R]egular or [P]remium:" << "\n";
cin >> servcode;
while ((servcode …Run Code Online (Sandbox Code Playgroud) 有人可以解释这个奇怪的python行为吗?显然字符串'test'不包含'Bid'或'Ask'.为什么这匹配?
import re
pat=r'[Bid|Ask]'
reg=re.compile(pat)
if reg.search('test'): print "matched!"
Run Code Online (Sandbox Code Playgroud)
......匹配!
试图理解Java中的upcasting.最近发现奇怪的行为.
例:
public class A extends B {
public int i = 2;
public void printI() {
System.out.println("print i = " + this.i);
}
public static void main(String[] args) {
B a = new A(); // <- upcasting here
System.out.println("i = " + a.i);
a.printI();
}
}
class B {
public int i = 1;
public void printI() {}
}
//Output:
//i = 1
//print i = 2
Run Code Online (Sandbox Code Playgroud)
似乎,upcasted对象有两个独立的"i"属性.一个"i"可以直接访问(ai),另一个通过子类(a.printI())的方法访问.
看起来像upcasted对象从超类和子类的方法获取属性.
对象如何拥有两个独立的"我"?!
#include<iostream>
using namespace std;
void main()
{
char fname[11];
int x = 0;
cout << "Please enter the name: ";
cin >> fname;
while (fname[x] != '\0')
{
int i=int(fname[x]);
if (i>=97)
cout << fname[x];
x++;
}
else
cout << "Invalid characters";
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用上面的代码验证char输入.但是做不到.
这些代码有什么问题?
我对malloc有一个奇怪的问题.我有这个typedef:
typedef struct buffer {
int D;
int T;
unsigned int current_size;
unsigned int max_size;
pthread_mutex_t mutex;
pthread_cond_t non_pieno;
pthread_cond_t non_vuoto;
msg_t** messages;
struct buffer* (*buffer_init)(unsigned int);
void (*buffer_destroy)(struct buffer*);
} buffer_t;
Run Code Online (Sandbox Code Playgroud)
这是buffer_init和buffer_destroy函数:
buffer_t* buffer_init(unsigned int maxsize)
{
buffer_t* new_buffer = (buffer_t*)malloc(sizeof(buffer_t));
msg_t** new_messages = (msg_t**)calloc(maxsize, sizeof(msg_t**));
pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t np = PTHREAD_COND_INITIALIZER;
pthread_cond_t nv = PTHREAD_COND_INITIALIZER;
new_buffer->T = 0;
new_buffer->D = 0;
new_buffer->current_size = 0;
new_buffer->max_size = maxsize;
new_buffer->messages = new_messages;
new_buffer->mutex = mx;
new_buffer->non_pieno = np; …Run Code Online (Sandbox Code Playgroud) 我编写此代码将文件'vetores'的内容复制到char矩阵,但我认为动态分配存在问题.有人可以帮帮我吗?
void cpyarqvetores( char** arqvetores, FILE* varquivo);
void freearqvetores( char** arqvetores );
int main()
{
FILE* varquivo;
varquivo = fopen("vetores.txt", "r");
char** arqvetores;
arqvetores = (char**) malloc(10*sizeof(char*));
cpyarqvetores( arqvetores, varquivo);
printf("%s\n", *(arqvetores + 4));//try prints this line
freearqvetores( arqvetores );
fclose( varquivo );
return 0;
}
//copy the contents of file 'vetores' to 'arqvetores'
void cpyarqvetores( char** arqvetores, FILE* varquivo){
char aux[440];
int strtam;
int i, j;
for( i = 0; i < 10; i++){
fgets( aux, 440, varquivo);
strtam …Run Code Online (Sandbox Code Playgroud) 这个C程序从键盘读取一行文本,然后在行中写入最长的单词.下面我的代码的问题是它只打印除了它的长度之外的最后一个单词,尽管一切看起来都很好.任何人都可以看到我的代码有问题吗?
#include<stdio.h>
#include<string.h>
#define MAX 132
#define MAXW 30
int Len_w[MAXW];
int Max_V(int vf[], int len);
main()
{
char s[MAX+1], w[MAXW], *Ind_w[MAXW],*p,out[MAXW];
int k=0, i=0, Maximum, g=0;
printf("\nInsert the line....\n");
p=fgets(s, MAX, stdin);
while(sscanf(p, "%s%n", w, &k)==1){
Len_w[i] = strlen(w);
Ind_w[i] = w; //the issue is here!!
p+=k+1;
i++;
}
Maximum = Max_V(Len_w,i);
for(g=0;g<i;g++){
if(Len_w[g] == Maximum){
//sscanf(Ind_w[g],"%s",out);
printf("\n%s", Ind_w[g]);
}
}
return 0;
}
/*----------------------------------------------------------------------------*/
int Max_V(int vf[], int len)
{
int j; int Max;
Max=*vf;
for(j=1; j < …Run Code Online (Sandbox Code Playgroud) 我工作的一个项目,但是当我从文件中读取它无法读取一些字符(如?,ž,š,等),我不知道我究竟做错了.
这是我的代码:
try {
reader = new InputStreamReader(getAssets().open("koce_podatki.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(reader);
for(int i=-1;i<position;i++){
try {
temp = "" + br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 好吧,温柔,因为我非常喜欢编程.到目前为止,我只学习了C++,并且我正在运行Visual Studio 2010作为我的编译器.对于这个程序,我试图从文本输入文件中读取并将信息写入一组三个数组.一个阵列将处理名称列表,另外两个阵列分别用于工作小时数和小时工资率.我将使用后两者来计算一组收入,并将整个事物输出到另一个文本文件作为报告.然而,我的问题是获取第一个数组的输入.我正在使用的输入文件的文本排列如下:
J. Doe*35 12.50 J. Dawn*20 10.00 .........
由于我试图使用ifstream getline来获取星号充当分隔符的名称,并将以下两个数字写入另外两个数组,因此这些名称会被星号所拖尾.后两个值由空格分隔,所以我认为它们不会引起任何问题.我确定还有其他错误需要处理,但我需要先处理第一个错误才能开始调试其余的错误.
我的问题出现在我调用inFile.getline的行,因为我收到以下错误:错误C2664:'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: getline(_Elem*,std :: streamsize,_Elem)':无法将参数1从'std :: string'转换为'char*'.
从我在其他地方读到的,(我认为)问题源于尝试将字符串写入char数组,由于它们具有不同的数据类型,因此无法工作.我不确定是否存在其他可行的方法来获取名称,因为我需要使用分隔符将名称与数值分开.如何解决这个问题的任何建议将不胜感激.
这是我写的来源:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int EMP_NUM = 5;
const int BASE_HOURS = 40;
const char N_SIZE = 8;
int main()
{
int i;
double rEarnings, oEarnings, tEarnings,
trEarnings, toEarnings, ttEarnings;
ifstream inFile;
ofstream outFile;
inFile.open("records.txt");
outFile.open("report.txt");
outFile << setprecision(2) << showpoint << fixed;
outFile << …Run Code Online (Sandbox Code Playgroud) 每次运行 pip install pyaudio 时都会出现以下错误
\npip3 install pyaudio\nCollecting pyaudio\n Using cached PyAudio-0.2.12.tar.gz (42 kB)\n Installing build dependencies ... done\n Getting requirements to build wheel ... done\n Preparing metadata (pyproject.toml) ... done\nBuilding wheels for collected packages: pyaudio\n Building wheel for pyaudio (pyproject.toml) ... error\n error: subprocess-exited-with-error\n \n \xc3\x97 Building wheel for pyaudio (pyproject.toml) did not run successfully.\n \xe2\x94\x82 exit code: 1\n \xe2\x95\xb0\xe2\x94\x80> [16 lines of output]\n running bdist_wheel\n running build\n running build_py\n creating build\n creating build/lib.macosx-10.9-universal2-cpython-310\n copying src/pyaudio.py -> build/lib.macosx-10.9-universal2-cpython-310\n …Run Code Online (Sandbox Code Playgroud)