0

Please help me fix this code

#include <iostream> #include<cmath> #include<string> #include<sstream> #include<fstream> using namespace std; void displayValues(const int*,int) int main() { const int SIZE1=4; const int SIZE2=3; const int array1[SIZE1]={1,2,3,4}; const int array2[SIZE2]={6,7,9}; string khumo; string filename="values.txt"; fstream nkuli; nkuli.open("values.txt"); displayValues (array1 ,SIZE1 ); displayValues (array2 ,SIZE2 ); nkuli.close(); return 0; } void displayValues (const int*numbers,int size) { int maxElement; int num; for(int count=0;count <size;count++) { if(maxElement =num%2!=0 { cout<<*((numbers+count)^2)<< " "; } else(maxElement =num%2==0); { cout<<*((numbers +count)*maxElement)<< " "; } } cout<<endl; }

16th Jun 2020, 9:21 PM
Nonkululeko
4 Antworten
0
I am not sure what you are trying to achieve, but you have a lot of syntax errors. A real mess! 😨 • Missing space: #include <cmath> void displayValues(const int *numbers, int size) {..} • Instructions end with a semicolon ';': void displayValues(const int*, int); • I am not sure if you can use an assignment to a variable in the "if" statement, so I try to avoid such things and use two separate statements. This also helps to read the source code easier: if (maxElement = num % 2 != 0) {..} • Adding a pointer with an integer will increment pointer value (change the address of the memory it's pointing) not the value of the variable on that address: cout << *((numbers + count) ^ 2) << " ";
17th Jun 2020, 1:24 AM
Vasile Eftodii
Vasile Eftodii - avatar
0
The question was to write a program that will read from a file called values.txt with the values that I included in the array .Then if the largest element in the array is odd,square every value in the array and display the array as a single comma delimited line with new line at the end.If the largest element in the array is even ,multiply every value by the largest value in the array and display the array as a single comma delimited line with new line at the end. #include <iostream> #include<cmath> #include<string> #include<sstream> #include<fstream> using namespace std; void displayValues(const int*,int) int main() { const int SIZE1=4; const int SIZE2=3; const int array1[SIZE1]={1,2,3,4}; const int array2[SIZE2]={6,7,9}; string khumo; string filename="values.txt"; fstream nkuli; nkuli.open("values.txt"); displayValues (array1 ,SIZE1 ); displayValues (array2 ,SIZE2 ); nkuli.close(); return 0; } void displayValues (const int*numbers,int size) { int maxElement; int num; for(int count=0;count <s
17th Jun 2020, 10:01 AM
Nonkululeko
0
About operators: http://www.cplusplus.com/doc/tutorial/operators https://docs.microsoft.com/en-us/cpp/cpp/for-statement-cpp?view=vs-2019 Both forms are correct, but ++counter in a for statement it's more common in use. Pointers are variables which store RAM addresses, not values. They are pointing to an address. Arrays are pointers by themselves, and points to the address of the first element, so we can access them inside the function as we do with normal arrays outside of them. https://www.programiz.com/cpp-programming/pointers-arrays Read pages 8-9 here: https://www.google.com/url?sa=t&source=web&rct=j&url=https://pages.cpsc.ucalgary.ca/~jacob/Courses/Fall00/CPSC231/Slides/13-Functions.pdf&ved=2ahUKEwj2nrmG_YnqAhUGYcAKHWF6BjUQFjAMegQIAhAB&usg=AOvVaw3-ttW6zA1R3glEsMjfMTqJ
17th Jun 2020, 10:22 PM
Vasile Eftodii
Vasile Eftodii - avatar
0
Okay thank you
20th Jun 2020, 4:58 PM
Nonkululeko