I want to send an array to a function then print all of the elements. I have an error.
.LST file is:
BACI System: C-- to PCODE Compiler, 16:59 27 Oct 2005
Source file: 1.cm Fri Nov 01 03:16:20 2019
line pc
1 0
2 0
3 0
4 0 void Print(int a[])
Error near '[', line 4 of file 1.cm:
** syntax error
Because of 1 error the PCODE file will not execute
Baci reference is:
http://inside.mines.edu/~tcamp/baci/baci_index.html
C:\badosxe> bacc 1.cm Error near '[', line 4 of file 1.cm: ** syntax error>>Because of 1 error the PCODE file will not execute Pcode and tables are stored in 1.pco Compilation listing is stored in 1.lst
void Print(int a[])
{
int i;
for (i = 0; i < 5; i = i + 1)
{
cout<<a[i];
}
}
main()
{
cout << endl;
cobegin
{
int a[5] = {1,2,3,4,5};
Print(a);
}
}
Related
With Qt 6.3.1 and MSVC 2019 (16.11.16) the following code crashes in assignment operator:
#include <QHash>
void fillHash( QHash< int, QString > & hash )
{
const auto str = QStringLiteral( "abc" );
for( qsizetype i = 0; i < 99; i += 3 )
hash[ i ] = hash[ i + 1 ] = hash[ i + 2 ] = str;
}
int main()
{
QHash<int, QString> h;
fillHash(h);
return 0;
}
It crashes with the following stack:
1 std::_Atomic_integral<int,4>::operator++ atomic 1587 0x7ff953ee7279
2 QAtomicOps<int>::ref<int> qatomic_cxx11.h 281 0x7ff953ee3c74
3 QBasicAtomicInteger<int>::ref qbasicatomic.h 101 0x7ff953eed687
4 QArrayData::ref qarraydata.h 88 0x7ff953eed6b7
5 QArrayDataPointer<char16_t>::ref qarraydatapointer.h 325 0x7ff953eed652
6 QArrayDataPointer<char16_t>::QArrayDataPointer<char16_t> qarraydatapointer.h 72 0x7ff953ee40cd
7 QArrayDataPointer<char16_t>::operator= qarraydatapointer.h 93 0x7ff9541ca864
8 QString::operator= qstring.cpp 2825 0x7ff95422d891
9 fillHash main.cpp 9 0x7ff67d9c2d03
10 main main.cpp 18 0x7ff67d9c2dcc
11 invoke_main exe_common.inl 79 0x7ff67d9ca109
12 __scrt_common_main_seh exe_common.inl 288 0x7ff67d9c9fee
13 __scrt_common_main exe_common.inl 331 0x7ff67d9c9eae
14 mainCRTStartup exe_main.cpp 17 0x7ff67d9ca19e
15 BaseThreadInitThunk KERNEL32 0x7ff9b38c7034
16 RtlUserThreadStart ntdll 0x7ff9b4e62651
What's wrong?
This question already has answers here:
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 2 years ago.
I have a problem with this program: I was able to make another draft program but it only reads the first three lines and stops. Unfortunately, the input file is not symmetrical so I should use while loops for some parts only to avoid repeating parts of the program. With strcpy I could copy the work done up to a certain point and then continue from that point on. I was trying but visual studio tells me to move the memory to the heap but I don't know how to do it ... Practically I should only extrapolate certain data from the input file and order them in an output file according to a precise line.
I also know there's a problemi with char buf[200], rec[200] because I have to allocate much more memory...
the first time in
while ( !feof(fd) ) {
fscanf(fd, "session %d (COPY MODE):\n\n", &session);
you call feof before any read so feof cannot work proprely, an easy change is to do
while (fscanf(fd, "session %d (COPY MODE):\n\n", &session) == 1) {
Out of that the mode can be something else that COPY MODE and the two \n after are not mandatory, you also do not check your next fscanf success and globaly the algorithm you use is wrong
A possible way to do the job also checking the validity of the input file is :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE * fp = fopen("summary.txt", "r");
if (fp == NULL) {
perror("cannot open summary.txt");
exit(1);
}
int session;
char mode[64];
const char * separator = "_____________________________________________";
while (fscanf(fp, " session %d (%[^)]):", &session, mode) == 2) {
if (strcmp(mode, "COPY MODE") && strcmp(mode,"ONLINE FREE MODE"))
{
fprintf(stderr, "invalid file, invalid mode '%s'\n", mode);
exit(1);
}
char line[64];
char given[64];
char recognized[64];
int registration, registration_set = 0;
int sequences, sequences_set = 0;
for(;;) {
/* read next line, bypass possible newline(s) before */
if (fscanf(fp, " %63[^\n]", line) != 1) {
if (!feof(fp)) {
perror("reading file");
exit(1);
}
*line = 0;
}
else if (sscanf(line, "number of sequences: %d", &sequences) == 1) {
sequences_set = 1;
continue;
}
else if ((*given == 0) &&
(sscanf(line, "characters given: %63s", given) == 1)) {
if (!registration_set) {
fprintf(stderr,
"registration is missing before 'characters given: %s'\n",
given);
exit(1);
}
if (!sequences_set) {
fprintf(stderr,
"sequences is missing before 'characters given: %s'\n",
given);
exit(1);
}
continue;
}
else if ((*recognized == 0) &&
(sscanf(line, "characters recognized: %63s", recognized) == 1)) {
if (!*given) {
fprintf(stderr,
"given is missing before 'characters recognized: %s'\n",
recognized);
exit(1);
}
continue;
}
if (registration_set) {
if (!*given) {
fputs("invalid file, given is missing\n", stderr);
exit(1);
}
printf("%d %d %d %s %s\n", session, registration, sequences, given, recognized);
if (!*line || !strcmp(line, separator))
break;
}
if (sscanf(line, "registration %d:", ®istration) != 1) {
fprintf(stderr,
"invalid file, 'registration <n>:' expected rather than '%s'\n",
line);
exit(1);
}
else {
registration_set = 1;
*given = *recognized = 0;
}
}
}
fclose(fp);
return 0;
}
Note the space at the beginning of the format of the fscanf, that allows to bypass character considered to be space including the newline
Compilation and execution :
pi#raspberrypi:/tmp $ gcc -Wall c.c
pi#raspberrypi:/tmp $ cat summary.txt
session 1 (COPY MODE):
number of sequences: 15
registration 1:
characters given: CALOR
registration 2:
characters given: CARINO
registration 3:
characters given: SUSHI
_____________________________________________
session 2 (COPY MODE):
registration 1:
number of sequences: 15
characters given: SUSHI
characters recognized: SUSHI
_____________________________________________
session 3 (ONLINE FREE MODE):
number of sequences: 15
registration 1:
characters given: PERA
characters recognized: PFRA
registration 2:
characters given: SALON
characters recognized: SALON
registration 3:
characters given: PERRO
characters recognized: PERRO
_____________________________________________
session 4 (ONLINE FREE MODE):
registration 1:
number of sequences: 7
characters given: TORTUGA
characters recognized: TORTUGA
registration 2:
number of sequences: 4
characters given: ANAEROBIO
characters recognized: ANAERPBIO
registration 3:
number of sequences: 4
characters given: PAPELES
characters recognized: PAPELEX
pi#raspberrypi:/tmp $ ./a.out
1 1 15 CALOR
1 2 15 CARINO
1 3 15 SUSHI
2 1 15 SUSHI SUSHI
3 1 15 PERA PFRA
3 2 15 SALON SALON
3 3 15 PERRO PERRO
4 1 7 TORTUGA TORTUGA
4 2 4 ANAEROBIO ANAERPBIO
4 3 4 PAPELES PAPELEX
pi#raspberrypi:/tmp $
I executed the following code on Ubuntu 14.04 and CentOS 7 using gcc compiler but the strange thing is that it shows different output for same inputs. There are two issues that I'm unable to solve.
I always get 0 in sum(in main function) for the very first multiplication (1*1).
Completely unexpected output for Ubuntu.
Here is the code and both the outputs.
Code
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#define N 15
struct matrix
{
int num1, num2;
};
void* multiply(void *c);
int main()
{
int i, j, rows, cols, a[N][N], b[N][N], sum, k, final, res[N][N],*ptr;
pthread_t t1, t2;
struct matrix m1;
ptr=∑
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of cols: ");
scanf("%d", &cols);
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("Enter the value at: a[%d][%d] : ", i, j);
scanf("%d", &a[i][j]);
}
}
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("Enter the value at: b[%d][%d] : ", i, j);
scanf("%d", &b[i][j]);
}
}
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
final = 0;
for(k = 0; k < rows; k++)
{
m1.num1 = a[i][k];
m1.num2 = b[k][j];
pthread_create(&t1, NULL, (void*)multiply,(void*)&m1);
pthread_join(t1, (void**)&ptr);
sum=*ptr;
printf("\t%d",sum);
final += sum;
res[i][j] = final;
}
printf("\n");
}
}
printf("The result is :\n");
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
printf("%d\t", res[i][j]);
}
printf("\n");
}
return 0;
}
void* multiply(void *c)
{
struct matrix *m;
m = (struct matrix *)c;
int p = 0;
p = m->num1 * m->num2;
printf("\t%d * %d = %d",m->num1,m->num2,p);
pthread_exit((void*)&p);
}
Output for execution on Ubuntu
Enter the number of rows: 2
Enter the number of cols: 2
Enter the value at: a[0][0] : 1
Enter the value at: a[0][1] : 2
Enter the value at: a[1][0] : 3
Enter the value at: a[1][1] : 4
Enter the value at: b[0][0] : 1
Enter the value at: b[0][1] : 2
Enter the value at: b[1][0] : 3
Enter the value at: b[1][1] : 4
1 * 1 = 1 0 2 * 3 = 6 32648
1 * 2 = 2 32648 2 * 4 = 8 32648
3 * 1 = 3 32648 4 * 3 = 12 32648
3 * 2 = 6 32648 4 * 4 = 16 32648
The result is :
32648 65296
65296 65296
Output for execution on CentOS
Enter the number of rows: 2
Enter the number of cols: 2
Enter the value at: a[0][0] : 1
Enter the value at: a[0][1] : 2
Enter the value at: a[1][0] : 3
Enter the value at: a[1][1] : 4
Enter the value at: b[0][0] : 1
Enter the value at: b[0][1] : 2
Enter the value at: b[1][0] : 3
Enter the value at: b[1][1] : 4
1 * 1 = 1 0 2 * 3 = 6 6
1 * 2 = 2 2 2 * 4 = 8 8
3 * 1 = 3 2 4 * 3 = 12 4
3 * 2 = 6 3 4 * 4 = 16 16
The result is :
6 10
15 22
The multiply function is returning a pointer to a local variable p, and the lifetime of the local variable finishes as soon as the function ends.
The easiest solution here is not to use the return value, but to reserve a place for the result in the struct matrix that is passed to multiply(), because that structure is allocated within main. Change the definition of struct multiply:
struct matrix
{
int num1, num2;
int product;
};
Change multiply() to put the result here:
void *multiply(void *c)
{
struct matrix *m = c;
m->product = m->num1 * m->num2;
printf("\t%d * %d = %d", m->num1, m->num2, m->product);
return NULL;
}
Change main() to retrieve the result from there:
pthread_create(&t1, NULL, multiply, &m1);
pthread_join(t1, NULL);
sum = m1.product;
(Side note: that variable sum has a confusing name, since it doesn't hold a sum!)
//My Header File
//Header File
#include<iostream>
using namespace std;
MyFloat rotate_right(MyFloat obj)
{
/*All that this function is supposed to do, is to return a new object, that object will
be having array elements in the order like 2.2 3.2 4.2 5.2 6.2 where the
original(object that is passed in parameter) object must be having array elements
in order like 3.2 4.2 5.2 6.2 2.2
*/
MyFloat c;
c.size = obj.size;
c.arr = nullptr;
c.arr = new float [obj.size];
c.arr[0] = obj.arr[obj.size - 1];
for(int i = 1,j=0; i < (obj.size - 1); i++,j++)
{
c.arr[j] = obj.arr[i];
}
return c;
}
class MyFloat
{
public:
MyFloat();//Default Constructor
MyFloat(int data);//Parameterized Constructor
MyFloat(MyFloat & const obj);//Copy Constructor
void input();//Filling Array
void PrintFloat();//For Displaying Array
MyFloat& operator=(MyFloat & const obj);//Assignment Operator Overloading
friend MyFloat rotate_right(MyFloat obj);/////////////This is the Function which is causing Problem/////////////
~MyFloat();//Destructor
private:
int size;//size of array
float *arr;
};
////////////This is the Cpp FILE Having Definition of All Functions
#include"My_Floats_Header.h"
MyFloat::MyFloat()
{
cout << "Default Constructor Called!" << endl;
size = 0;
arr = nullptr;
}
MyFloat::MyFloat(int _size)
{
cout << "Parametrized Constructor Called!" << endl;
size = _size;
arr = nullptr;
arr = new float [_size];
}
void MyFloat :: input()
{
for(int i = 0 ; i < size; i++)
{
cout << "Enter an Element = ";
cin >> arr[i];
}
}
MyFloat :: MyFloat(MyFloat & const obj)
{
size = obj.size;
arr = nullptr;
arr = new float [size];
for(int i = 0 ; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
}
MyFloat & MyFloat :: operator=(MyFloat & const obj)
{
size = obj.size;
arr = nullptr;
arr = new float [size];
for(int i = 0 ; i < obj.size; i++)
{
arr[i] = obj.arr[i];
}
return *this;
}
void MyFloat :: PrintFloat()
{
for(int i = 0 ; i < size; i++)
{
cout << arr[i];
}
cout << endl;
}
MyFloat::~MyFloat()
{
if(arr != nullptr)
{
delete [] arr;
}
size = 0;
}
////Cpp file having mian()
#include"My_Floats_Header.h"
int main()
{
MyFloat *floatNumber = new MyFloat(5);//declaration
floatNumber -> input();//taking input
MyFloat newfloatNumber = *floatNumber;
floatNumber = rotate_right(floatNumber);
cout << "My Float without Rotation: ";
newfloatNumber.PrintFloat();
cout << "My Float after Rotation: ";
floatNumber -> PrintFloat();
system ("pause");
return 0;
}
ERRORS THAT I'm GETTING:
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (in line number 4)
Error C2146 syntax error: missing ';' before identifier 'rotate_right' (in line number 4)
Error C2143 syntax error: missing ';' before '{' (in line number 5)
Error C2447 '{': missing function header (old-style formal list?) (in line number 5)
Error C2143 syntax error: missing ';' before '&' (in line number 40)
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (in line number 40)
Error C2086 'int MyFloat': redefinition (in line number 40)
Error C2761 'MyFloat &MyFloat::operator =(MyFloat &)': member function redeclaration not allowed (in line number 41)
Error C2059 syntax error: '{' (in line number 41)
Error C2143 syntax error: missing ';' before '{' (in line number 41)
Error C2447 '{': missing function header (old-style formal list?) (in line number 41)
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (in line number 4)
Error C2146 syntax error: missing ';' before identifier 'rotate_right' (in line number 4)
Error C2143 syntax error: missing ';' before '{' (in line number 5)
Error C2447 '{': missing function header (old-style formal list?) (in line number 5)
Error C2065 'floatNumber': undeclared identifier (in line number 6)
Error C2061 syntax error: identifier 'MyFloat' (in line number 6)
Error C2065 'floatNumber': undeclared identifier (in line number 7)
Error C2227 left of '->input' must point to class/struct/union/generic type (in line number 7)
Error C2146 syntax error: missing ';' before identifier 'newfloatNumber' (in line number 8)
Error C2065 'newfloatNumber': undeclared identifier (in line number 8)
Error C2065 'floatNumber': undeclared identifier (in line number 8
)
Error C2065 'floatNumber': undeclared identifier (in line number 9)
Error C2065 'newfloatNumber': undeclared identifier (in line number 12)
Error C2228 left of '.PrintFloat' must have class/struct/union (in line number 12)
Error C2065 'floatNumber': undeclared identifier (line number 15)
Error C2227 left of '->PrintFloat' must point to class/struct/union/generic type (line number 15)
MyFloat rotate_right() probably shouldn't be in a header file. If you take it out, you should resolve (most? all?) of your errors.
My recommendation would be to make it a static method of MyFloat.
As an alternative, you could keep rotate_right() as a standalone function, and simply #include "MyFloat.h at the top of your .cpp file.
In the header file, move the class definition above the definition of rotate_right(). This will get you closer to working code. Order matters.
//This code gives randomly generated alphabets and if equal will
//cout the alphabet which is equal
1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 using namespace std;
5 int main()
6 {
7 int a;
8 char array[10];
9 srand (time(0));
10 for (int i=0; i<10; i++)
11 {
12 a=rand() %26;
13 a=a+97;
14 array[i]=char(a);
15 }
16 for (int i=0; i<10; i++)
17 {
18 for (int j=0; j<10; j++)
19 {
20 if (array [i]==array [j]);
21 {
22 cout <<array[i] <<" " <<array[j];//if alphabet 'c' is at indecis 2,5,8
//then output should be like that of
// only 22 no statement but actually it does
// not give this answer but gives wrong output
//c c
//c c
//c c
23 }
24 cout << endl;
25 }
26 }
27 return 0;
28 } //program end
My question is how to check that randomly generated alphabets are equal e.g in 22 number line it should give output of equal alphabets if they are equal but it does not give equal alphabets what wrong in this code mention the wrong statement or help me how will i get right answer
Actually i want to make a program that should tell me how many times a single generated alphabet comes in an array
According to your comment you're looking for something that does this:
#include <random>
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
int main(){
::std::vector<char> letters(10);
::std::vector<char> unique;
::std::random_device rd;
::std::mt19937 mt(rd());
::std::uniform_int_distribution<char> dis('a', 'z');
auto random = ::std::bind(dis, mt);
// fill the vector with random letters
for(char & l : letters){
l = random();
}
int max_count = 0;
char appeared_most = ' ';
// get a vector of all the unique values
::std::unique_copy(letters.cbegin(), letters.cend(), ::std::back_inserter(unique));
// find the first value to appear most if two values appear equally
// the first will be chosen
for(const char & l : unique){
int count = ::std::count(letters.cbegin(), letters.cend(), l);
if(count > max_count){
max_count = count;
appeared_most = l;
}
}
::std::cout << appeared_most << " " << max_count ;
}
I have a working sample here: http://coliru.stacked-crooked.com/a/53c953576e0db756