c++ - function requires c style array [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I need to use some static c-libraries in my c++ code.
There I have a lot of functions which look like this (here I use a very simple example in this library to create a rotation matrix about the z-axis with the angel alpha):
void Rz(double alpha, double r[3][3])
In the end, r should be the final matrix.
I tried to call the function like this:
double alpha = 1.234;
double R[3][3] = {};
iauRz(alpha, R);
for(int i = 0; i<3; ++i){
cout << " " << R[i][0] << " " << R[i][1] << " " << R[i][2] << "\n";
}
and I hoped to display the rotation matrix, but this doesn't work. My result is:
0 0 0
0 0 0
0 0 0
Is there a mistake how I call the function? I'm not familiar with this c-style arrays...
EDIT: The library offers some very complex functions, the rotation about the z-axis was just a example. This means it is no possibility to use a more c++ style library.
EDIT 2: indices changed

You're using indices starting from 1. Indices start from 0 in C/C++
for(int i = 0; i<3; ++i){
cout << " " << R[i][0] << " " << R[i][1] << " " << R[i][2] << "\n";
}

Indices are zero based in almost every language and this has a reason: R[i] in C++ (and C) can also be written as
*(R + i * sizeof(datatype))
which means "take the memory address of R increased i times by the size of the datatype of the array and get the value at that address", so it makes perfect sense to have the first object of the array at R[0], because this is the memory address of R plus 0 bytes, so the first element

Related

For loop displaying incorrect information stored inside array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have this really simple program, with a for loop designed to run 3 times.
For every iteration, it will ask the user to enter in the weather. Each time, the user's input is stored in an integer array called C.
Once the loop concludes, I have another for loop that print's out the user's input. This next loop works fine for the 2 values, but gives off some weird messed up value once it reaches the third iteration.
int main(){
//Variable declaration:
int days;
int C[2];
int F[2];
for(days=0; days<3; days++){
cout << "What is the temperature in celsius for day " << days + 1 << ":" << endl;
cin >> C[days];
F[days] = (C[days] * 9/5) + 32;
}
cout << "\nCelsius\t\t Farenheit\n-------\t\t ---------" << endl;
for(days =0; days <3; days++){
cout << C[days] << "\t\t " << F[days] << endl;
}
return 0;
}
This next loop works fine for the 2 values but gives off some weird messed up value once it reaches the third iteration.
Your definition of C states it has 2 entries, but your loop runs for
three iterations (0, 1, and 2).

How to print a char array and a certain character in it? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I've been reviewing my C++ lately. But I am running into a puzzle about printing a char array. The code is below:
int n = 5;
char *array1 = new char[n];
for (unsigned int i = 0; i < n - 1; i++)
array1[i] = (char)i;
cout << array1 << endl;
cout << array1[3] << endl;
cout << *array1 << endl;
None of the three cout lines works. Could anyone tell me why?
array1[0] == 0. cout << array1 interprets array1 as a pointer to a NUL-terminated string, and since the very first character is in fact NUL, the string is empty.
cout << array1[3] does print a character with ASCII code 3. It's a non-printable character, not visible to a naked eye. Not sure what output you expected to see there.
As a separate answer, it seems you're trying to get a string which has the following : array = "1234....(n-1)"
Try :
for (int i = 0; i<(n-1); i++)
array1[i] = (char)i - '0';

From the user's input total the odd and even numbers. input numbers in between 0 to 99 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int i,t,x[20], even, odd, prime;
cout << "Enter 20 integer numbers from 0 to 99: "<<endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i <<":";
cin >> x[i];
}
cout << "\nPrime numbers are: " << endl ;
prime=1;
for (i=2; i<=20 ; i++)
{
for(t=2;t<x[i];t++)
{
if(x[i]%t==0)
{
prime=0;
}
}
if(prime==1)
{
cout << x[i] << endl;
}
prime=1;
}
for(i=1; i<=20; i++) // this is where i have problem.
{
if(x[i]% 2 == 0)
{
even++;
}
else
{
odd++;
}
}
cout << "Number of odd numbers: " << odd << "\n";
cout << "Number of even numbers: " << even << "\n";
return 0 ;
}
When i compile it shows even (40) and odd (10) for input of 0 till 19. Where it should show even 10(including the 0) and odd (10). Im not sure where am i doing it wrongly. I hope someone can help me improve the code.
Variables even and odd are never set to a known value, so you are not formally allowed to read from them. Doing so invokes that most infamous Standardese concept: undefined behaviour. So the values of these variables could be right or could be wrong; the variables and all code trying to read them could be optimised entirely out of your program; or anything can happen. You cannot rely on these variables doing anything right. All attempts to read them make your program ill-formed, so now it can do anything, including things you would never have imagined.
You should search for the abundant background info about these concepts, but I like to think I made a fairly decent summary here: https://stackoverflow.com/a/38150162/2757035
Also, as Thomas points out in the comments, you appear not to understand how array indexing works: Indexes are 0-based. So, int i[20] declares 20 elements numbered from 0 to 19. You try to access index 20, which is not part of the array and hence is more undefined behaviour.

Not entering while loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
[EDIT] - Nevermind, this is a really dumb question
I'm currently trying to create a program that will basically tell me when insertion sort takes a longer time than merge sort for given a, b and n (in this case, successive powers of 2). This is what I have so far:
int comparisonSort()
{
//prompt user for values of a and b
int a = 0;
int b = 0;
cout << "Enter the value for a" << endl;
cin >> a;
cout << "Enter a value for b" << endl;
cin >> b;
double insertionSortTime = 0;
double mergeSortTime = 0;
double n = 2;
cout << outside while loop" << endl; //check
while (insertionSortTime < mergeSortTime)
{
cout << "inside while loop" << endl; //check
//compute the insertion and merge sort execution times
insertionSortTime = a*n*n;
mergeSortTime = b*n*log2(n);
cout << "is = " << insertionSortTime << " ms = " << mergeSortTime << endl;
n = pow(n, 2); // n^2
}
cout << "value of n that insertion sort beat merge sort is: " << n << endl;
return 0;
}
when I run this, I get:
Enter the value for a
8
Enter a value for b
64
outside while loop
value of n that insertion sort beat merge sort is: 2
I have no idea why the while loop isn't getting executed... Sorry if this seems like a really simple question, but I'm new to C++ and any help would be greatly appreciated, thank you!!
The conditional in
while (insertionSortTime < mergeSortTime)
is false in the first iteration when both insertionSortTime and mergeSortTime are set to zero. That explains why the loop never got executed.
Perhaps you meant to use:
while (insertionSortTime <= mergeSortTime)
Its because you have insertionSortTime = 0 and mergeSortTime = 0 and the condition for your loop is insertionSortTime < mergeSortTime.
Of course 0 is not < 0 so it never enters the loop.
Change it to <=.

Run time error when running very simple program (C++) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I wrote a program as part of a learning exercise that takes doubles as inputs, and outputs the total sum of all the doubles, as well as the smallest, largest, and the mean. When trying to run the program, I get this run time error:
Unhandled exception at 0x74b01d4d in Hello World.exe: Microsoft C++ exception: Range_error at memory location 0x00dcf760.
When I use the Visual Studio 2010 debug option, I get vector subscript is out of range.
The code is:
#include "C:\Users\Kevin\Documents\Visual Studio 2010\Projects\std_lib_facilities.h"
int main() {
vector<double> v;
double number = 0;
cout << "Enter the distance between two cities along the route.\n";
while (cin >> number) {
double sum = 0;
v.push_back(number);
sort(v.begin(),v.end());
for (size_t i = 0; i < v.size(); ++i)
sum += v[i];
cout << "The total distance from each city is " << sum
<< ". The smallest distance is " << v[0]
<< " and the greatest distance is " << v[v.size()]
<<". The mean distance is " << sum/v.size() <<".\n";
cout << "Enter the distance between two cities along the route.\n";}
}
I had to change the type of the variable defined in the for loop because I was getting a signed/unsigned mismatch error. The code gives no errors when compiled and I am having difficulty seeing the problem.
" and the greatest distance is " << v[v.size()]
is an undefined behavior because the last index in vector is v.size() - 1 if v.size() > 0 (if v.size() = 0 there is no items and nothing to subscript)
You should thus write:
if( !v.empty())
std::cout << " and the greatest distance is " << v[ v.size() - 1];
or better;
if ( !v.empty()) {
std::cout << " and the greatest distance is " << v.back();
std::vector::back