Passing multiple variables in a function? - function

I'm a first year CS student trying to understand functions in C++ better because I am weak in that area as of right now. I'm trying to create a program that will ask the user for two integers that will then be passed to a calculation function that will finally be passed to a display function to show the calculations. As of right now here is my code with the output at the bottom. I'm not really sure why the num1 and num2 aren't properly being passed to the calculation function? Any help is appreciated and please disregard the style, I usually try and clean it up after I get it to work.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void getData();
void doTheMath(int num1, int num2);
void displayResults(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem);
int main()
{
int num1;
int num2;
int sum;
int diff;
int prod;
int quot;
int rem;
getData();
doTheMath(num1, num2);
displayResults(num1, num2, sum, diff, prod, quot, rem);
system("pause");
return 0;
}
void getData()
{
int num1;
int num2;
cout << "Please enter two integer values:\n";
cin >> num1;
cin >> num2;
cout << "The first number is " << num1
<< " and the second is "<< num2 << "\n\n";
}
void doTheMath(int num1, int num2)
{
int sum = num1 + num2;
int diff = num1 - num2;
int prod = num1 * num2;
int quot = num1 / num2;
int rem = num1 % num2;
}
void displayResults(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem)
{
if (num2 == 0)
{
cout << "Here are the results:\n\n";
cout << "The sum of " << num1 << " and " << num2
<< " is " << sum << ".\n";
cout << "The difference, (" << num1 << " minus "
<< num2 << ") is " << diff << ".\n";
cout << "The product of " << num1 << " and "
<< num2 << " is " << prod << ".\n";
cout << "Cannot divide by zero.\n\n";
}
else
{
cout << "Here are the results:\n\n";
cout << "The sum of " << num1 << " and " << num2
<< " is " << sum << ".\n";
cout << "The difference, (" << num1 << " minus "
<< num2 << ") is " << diff << ".\n";
cout << "The product of " << num1 << " and "
<< num2 << " is " << prod << ".\n";
cout << num1 << " divided by " << num2 << " is "
<< quot << " with a remainder of " << rem
<< ".\n\n";
}
}
//Output
/*Please enter two integer values:
12
0
The first number is 12 and the second is 0
Here are the results:
The sum of -858993460 and -858993460 is -858993460.
The difference, (-858993460 minus -858993460) is -858993460.
The product of -858993460 and -858993460 is -858993460.
-858993460 divided by -858993460 is -858993460 with a remainder of -858993460.
Press any key to continue . . .*/

The num1 and num2 variables in main() are different variables than num1 and num2 in getData(). So you're setting these in getData() but doing nothing with them except displaying. The num1 and num2 in main() are not affected. Pass these (as a reference) to getData(int &num1, int &num2) and don't declare the ones in getData() itself. Read up in 'auto' variable declaration (declared on the stack).

//==========================================================
/*Description:
This program is to showcase my understanding of
functions that I learned from our Lecture 7b. The user
is prompted to enter two integer values, where they
are then passed to a calculation function to calculate
the sum, difference, product, quotient, and remainder
of the two numbers entered. After all the values are
calculated, they are showcased in a display function
to the user in the output stream.*/
//==========================================================
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void getData(int& num1, int& num2);
void doTheMath(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem);
void displayResults(int num1, int num2, int sum, int diff, int prod, int quot, int rem);
//====================== main ===========================
//
//=======================================================
int main()
{
int num1;
int num2;
int sum;
int diff;
int prod;
int quot;
int rem;
//Gets two integers from user
getData(num1, num2);
//Does the calculation from integers received
doTheMath(num1, num2, sum, diff, prod, quot, rem);
//Displays calculated results from two integers
displayResults(num1, num2, sum, diff, prod, quot, rem);
system("pause");
return 0;
}
/*===================== getData ==========================
This function gets the information from the user of the
two integers they wish to input. It assigns the user's
numbers to num1 and num2.
Input:
num1 - First integer assigned by user
num2 - Second integer assigned by user
Output:
The values being assigned to be used in the doTheMath
function.*/
//========================================================
void getData(int& num1, int& num2)
{
cout << "Please enter two integer values:\n";
cin >> num1;
cin >> num2;
}
/*==================== doTheMath =========================
This function calculates the user's two integers inputted
into the previous function and assigns the calculated
answers to variables named by the calculation performed.
It first checks to see if num2 is 0, because this system
can't divide by zero without crashing.
Input:
sum - adds the two integers
diff - subtracts the two integers
prod - multiplies the two integers
quot - divides the two integers
rem - gets the remainder of the two integers
Output:
Variables are now assigned new values to be displayed
inside of the displayResults function.*/
//========================================================
void doTheMath(int num1, int num2, int& sum, int& diff, int& prod, int& quot, int& rem)
{
if (num2 == 0)
{
sum = (num1 + num2);
diff = (num1 - num2);
prod = (num1 * num2);
}
else
{
sum = (num1 + num2);
diff = (num1 - num2);
prod = (num1 * num2);
quot = (num1 / num2);
rem = (num1 % num2);
}
}
/*================= displayResults ======================
This function takes the calculations from the doTheMath
function and displays them to the user in a standard
output stream. It first checks to see if num2 is 0,
because this system can't divide by zero without
crashing.
Input:
Calculations from the doTheMath function, as well as
num1 and num2.
(sum, diff, prod, quot, rem).
Output:
Displays the calculations from the doTheMath function
to the user in a standard output stream.*/
//========================================================
void displayResults(int num1, int num2, int sum, int diff, int prod, int quot, int rem)
{
if (num2 == 0)
{
cout << "Here are the results:\n\n";
cout << "The sum of " << num1 << " and " << num2
<< " is " << sum << ".\n";
cout << "The difference, (" << num1 << " minus "
<< num2 << ") is " << diff << ".\n";
cout << "The product of " << num1 << " and "
<< num2 << " is " << prod << ".\n";
cout << "Cannot divide by zero.\n\n";
}
else
{
cout << "Here are the results:\n\n";
cout << "The sum of " << num1 << " and " << num2
<< " is " << sum << ".\n";
cout << "The difference, (" << num1 << " minus "
<< num2 << ") is " << diff << ".\n";
cout << "The product of " << num1 << " and "
<< num2 << " is " << prod << ".\n";
cout << num1 << " divided by " << num2 << " is "
<< quot << " with a remainder of " << rem
<< ".\n\n";
}
}
//==========================================================
/*OUTPUT (When num2 != 0):
Please enter two integer values:
12
3
Here are the results:
The sum of 12 and 3 is 15.
The difference, (12 minus 3) is 9.
The product of 12 and 3 is 36.
12 divided by 3 is 4 with a remainder of 0.
Press any key to continue . . .*/
//==========================================================
//==========================================================
/*OUTPUT (When num2 == 0):
Please enter two integer values:
12
0
Here are the results:
The sum of 12 and 0 is 12.
The difference, (12 minus 0) is 12.
The product of 12 and 0 is 0.
Cannot divide by zero.
Press any key to continue . . .*/
//==========================================================

Related

How to create a function that outputs the largest number using conditions in c++

/*Description: Write a function called getMax that takes three parameters of type int, and returns the biggest of the
three parameters which is of type int.
*/
#include <iostream>
using namespace std;
// Declare the function getMax and put in three variables.
int getMax(int number, int number2, int number3){
if( number >= number2 && number >= number3){
cout << number;
if(number2 >= number && number2 >= number3)
cout << number2 ;
}
else {
cout << number3;
}
return number, number2, number3;
}
// we now use the function to check for largest values below:
int main(){
cout << getMax(-13, -22, -3) << endl; //Prints -3
cout << getMax(9, 8, 9) << endl; //prints 9
cout << getMax(-5, 4, -7) << endl; //prints 4
cout << getMax(15, 15, 15) << endl; //prints 15
return 0;
}
You can solve it this way:
int getMax(int number, int number2, int number3){
return number > number2
? (number > number3 ? number : number3)
: (number2 > number3 ? number2 : number3);
}
Without ternary operators you can write:
int getMax(int number, int number2, int number3){
if (number > number2) {
if (number > number3) {
return number;
}
return number3;
}
if (number2 > number3) {
return number2;
}
return number3;
}

Recursion and functions

Thanks for helping everyone. I will continue looking at it so I can better understand! I am still struggling with recursion but I will study it more. Thanks again for all your time and effort for trying to help me
- /
int countEven(int arr[i]){
//I'm not sure what to do here... how to fix it...
int evens = 0;
if(arr[i] <= 0) return 0; //base case
while(arr[i] > 0){
int digit = arr[i]%10; //get the last digit
if(digit%2 == 0){
evens = evens+1;
}
arr[i] = arr[i]/10;
}
cout << evens;
}
}
}
int main(){
cout << "Part A:\n";
int arr[3] = { 5050155, 5, 707070 };
for (int i = 0; i < 3; i++){
cout << "countEven(" << arr[i] << ") = " << countEven(arr[i]) << endl;
cout << "removeEven(" << arr[i] << ") = " << removeEven(arr[i]) << endl;
cout << "hasEven(" << arr[i] << ") = ";
if (hasEven(arr[i])) cout << "Yes" << endl;
else cout << "No" << endl;
printStarDigit(arr[i]);
cout << endl << endl;
}
cout << "Part B:\n";
int a[4] = { 7, 2, 8, 3 };
int b[5] = { 3, 4, 5, 6, 7 };
cout << "The range of array a is " << range(a, 4) << endl;
cout << "The range of array b is " << range(b, 5) << endl;
reverse(a, 4);
reverse(b, 5);
cout << "Array a reversed: ";
for (int i = 0; i < 4; ++i)
cout << a[i] << " ";
cout << endl;
cout << "Array b reversed: ";
for (int i = 0; i < 5; ++i)
cout << b[i] << " ";
cout << endl;
return 0;
}
int countEven(int arr[i]){
Parameters must have simple names, and do not need to be identical to the expressions passed in. arr[i] is not a valid name. A simple name to use here is n.
//I'm not sure what to do here... how to fix it...
int evens = 0;
if(arr[i] <= 0) return 0; //base case
This base case is wrong for two reasons. Firstly, you treat all negative integers as the base case, but -202020 has six even digits. Secondly, you return the wrong value: 0 has one even digit, but you return zero.
A possible base case could be n > -10 && n < 10 (single digit number). I'll let you figure out the expression to return for that base case.
while(arr[i] > 0){
If your task is to write a recursive function, then you shouldn't use a loop here. Instead, see below.
int digit = arr[i]%10; //get the last digit
...
arr[i] = arr[i]/10;
This is a correct way of obtaining the last digit, and everything other than the last digit.
if(digit%2 == 0)
This is a correct way of determining whether the last digit is even.
Now, you need to combine what you have, by observing that the count of even digits is equal to "1 if the last digit is even, else 0", plus the count of even non-last digits. The goal of the exercise is to get you to write "the count of even non-last digits" as countEven(n / 10).
See these code snippets.
int countEven(int n, bool first_time = true){
static int total ;
int num =n;
if (first_time)
{
total = 0;
}
if (num <= 9) {
return num % 2 == 0 ? 1 : 0;
}
int temp = num - (num / 10) * 10;
num = num / 10;
total += temp % 2 == 0 ? 1 : 0;
countEven(num,false);
return total;
}
int removeEven(int n){
int result = 0;
/*TODO*/
return result;
}
bool hasEven(int n){
return countEven(n);
}
void printStarDigit(int* arr){
/*TODO*/
}
int range(int* arr, int n){
int result = *arr;
for (int i = 0; i < n; i++) {
if (result < *(arr + i)) result = *(arr + i);
}
return result;
}
void reverse(int* arr, int n){
int* temp = new int[n];
for (int i = 0; i < n; i++) {
*(temp + i) = *(arr + i);
}
for (int i = 0; i < n; i++) {
*(arr + i) = *(temp + n - 1 - i);
}
delete[] temp;
}
Main implementation :
int main() {
cout << "Part A:\n";
int arr[3] = { 5050155, 5, 707070 };
for (int i = 0; i < 3; i++) {
cout << "countEven(" << arr[i] << ") = " << countEven(arr[i]) << endl;
cout << "removeEven(" << arr[i] << ") = " << removeEven(arr[i]) << endl;
cout << "hasEven(" << arr[i] << ") = ";
if (hasEven(arr[i])) cout << "Yes" << endl;
else cout << "No" << endl;
printStarDigit(arr + i);
cout << endl << endl;
}
cout << "Part B:\n";
int a[4] = { 7, 2, 8, 3 };
int b[5] = { 3, 4, 5, 6, 7 };
cout << "The range of array a is " << range(a, 4) << endl;
cout << "The range of array b is " << range(b, 5) << endl;
reverse(a, 4);
reverse(b, 5);
cout << "Array a reversed: ";
for (int i = 0; i < 4; ++i)
cout << a[i] << " ";
cout << endl;
cout << "Array b reversed: ";
for (int i = 0; i < 5; ++i)
cout << b[i] << " ";
cout << endl;
return 0;
}
Output:

Does the function in this code have an error?

This program is supposed to use a single function to return multiple values. However, since a function can return a single value at a time, I have implemented a switch. However, when I run the code, the gross pay, the taxes, and the net pay default to 0 instead of adding up. Does anyone know why this occurs? I am only allowed to use a single function with a switch to return the different values.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
double paycalculations(double, double, double, double, int);
double paycalculations(double hrs, double pay, double fedrate, double staterate, int option)
{
switch (option)
{
case 1:
double reg;
if (hrs < 0)
{
hrs = hrs * (-1);
}
if (hrs <= 40)
{
reg = hrs * pay;
}
else
{
reg = 40 * pay;
}
return reg;
break;
case 2:
double overtime;
overtime = (hrs - 40) * (1.5 * pay);
return overtime;
break;
case 3:
double gross;
if (overtime > 0)
{
gross = reg + overtime;
}
else
{
gross = reg;
}
return gross;
break;
case 4:
double ftaxes;
ftaxes = gross * fedrate;
return ftaxes;
break;
case 5:
double staxes;
staxes = gross * staterate;
return staxes;
break;
case 6:
double socialsectaxes;
socialsectaxes = gross * 0.062;
return socialsectaxes;
break;
case 7:
double medtaxes;
medtaxes = gross * 0.0145;
return medtaxes;
break;
case 8:
double net;
net = gross - (ftaxes + staxes + socialsectaxes + medtaxes);
return net;
break;
}
}
int main ()
{
string employeeID;
double hours, payrate, fedtaxrate, statetaxrate, regpay, overtimepay, grosspay, fedtaxes, statetaxes, sosectaxes, meditaxes, netpay;
const int SIZE = 100;
char again = 'y', fullname[SIZE] = {" "};
while (again == 'y')
{
hours = payrate = fedtaxrate = statetaxrate = regpay = overtimepay = fedtaxes = statetaxes = sosectaxes = meditaxes = netpay = 0;
system("reset");
cout <<"Employee Full Name: ";
cin.getline(fullname, SIZE);
cout << "\nEmployee ID: ";
cin >> employeeID;
cout << "\nHours Worked (Maximum of 60): ";
cin >> hours;
cout << "\nEmployee Payrate: $";
cin >> payrate;
cout << "\nFederal Tax Rate (Maximum of 0.5 or 50%): ";
cin >> fedtaxrate;
cout << "\nState Tax Rate (Maximum of 0.3 or 30%): ";
cin >> statetaxrate;
cout.setf(ios::fixed | ios::showpoint);
system("reset");
cout << "\t\t\t\t\tPayroll";
cout << "\n\nEmployee ID: " << employeeID;
cout << "\t\nEmployee Name: " << fullname;
cout.precision(1);
cout << "\n\nTotal Hours Worked: " << "\t\t\t\t" << hours;
cout.precision(2);
regpay = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 1);
if (hours <= 40)
{
cout << "\nRegular Hours Worked: " << hours << " # $" << payrate << "/hour" << "\t" << "$" << regpay;
}
overtimepay = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 2);
if (hours > 40)
{
cout << "\nRegular Hours Worked: " << "40.00 # $" << payrate << "/hour" << "\t" << "$" << regpay;
cout << "\nOvertime Hours Worked: " << (hours - 40) << " # $" << (payrate * 1.5) << "/hour" << "\t" << "$" << overtimepay;
}
grosspay = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 3);
cout << "\nGross Pay: " << "\t\t\t\t\t" << "$" << grosspay;
fedtaxes = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 4);
cout << "\nFederal Taxes Withheld #" << (fedtaxrate * 100) << "%" << "\t\t\t" << "$" << fedtaxes;
statetaxes = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 5);
cout << "\nState Taxes Withheld #" << (statetaxrate * 100) << "%" << "\t\t\t" << "$" << statetaxes;
sosectaxes = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 6);
cout << "\nSocial Security Withheld #" << (0.062 * 100) << "%" << "\t\t\t" << "$" << sosectaxes;
meditaxes = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 7);
cout << "\nMedicare Withheld #" << (0.0145 * 100) << "%" << "\t\t\t" << "$" << meditaxes;
netpay = paycalculations(hours, payrate, fedtaxrate, statetaxrate, 8);
cout << "\nNet Pay: " << "\t\t\t\t\t" << "$" << netpay;
cout << "\n\nWould you like to compute another employee's pay check? (Y or N): ";
cin >> again;
again = tolower(again);
}
}

Char Function, number to letter grades

I am relatively new to c++ programming and I am struggling with my code. The objective of this code is to take scores input by the user and calculate the mean, the standard deviation and converting it to a letter grade using the calculations under char gradeFunction. When i try to debug this program using visual studios 2013, i am having a couple problems with the the gradefunction. Again i am new to programming so troubleshooting errors is very hard for me and I would appreciate any help or advice! The program looks like this so far.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string.h>
#include <string>
using namespace std;
void printArray(int Array[], int count);
double average(double scoreTotal, int count);
double stddev(int Array[], int count, double mean);
char gradeFunction(int scores, double stddev, double mean);
int main()
{
int scores[8];
int count;
double scoreTotal = 0;
int standarddev[8];
double mean;
cout << "Enter scores seperated by blanks:" " ";
for (count = 0; count <= 7; count++)
{
cin >> scores[count];
scoreTotal += scores[count];
mean = scoreTotal / 8;
}
cout << endl;
cout << "Grade Scores by Student" << endl;
cout << "Score" "\t" "Grade" << endl;
cout << "----------------------------------" << endl;
printArray(scores, 8);
cout << gradeFunction(scores, stddev, mean);
cout << endl;
cout << "The mean is" " "<< fixed << setprecision(1) << average(scoreTotal, count) << endl;
cout << "The standard deviation is" " " << stddev(scores, count, mean) << endl;
cout << endl;
system("pause");
return 0;
}
void printArray(int Array[], int count)
{
for (int x = 0; x < count; x++)
{
cout << fixed << setprecision(1) << Array[x] << endl;
}
}
char gradeFunction(int scores, double stddev, double mean)
{
char F, D, C, B, A;
if (scores <= (mean - (1.5 * stddev)))
return 'F';
else if (scores <= (mean - (.5 * stddev)))
return 'D';
else if (scores <= (mean + (.5 * stddev)))
return 'C';
else if (scores <= (mean + (1.5 * stddev)))
return 'B';
else return 'A';
}
double average(double scoreTotal, int count)
{
return scoreTotal / count;
}
double stddev(int Array[], int count , double mean)
{
double stddev;
double sum2 = 0;
for (int i = 0; i < count; i++)
{
sum2 += pow((Array[i] - mean), 2);
}
stddev = sqrt(sum2 / (count - 1));
return stddev;
}
The error messages this leaves me with are...
3 IntelliSense: argument of type "double (*)(int *Array, int count, double mean)" is incompatible with parameter of type "double"
Error 1 error C2664: 'char gradeFunction(int [],double,double)' : cannot convert argument 2 from 'double (__cdecl *)(int [],int,double)' to 'double'

FFTW filled with zeros at the end

Can you help me find out why one of the FFTW's plans gives zeroes at the end of an output array? The "fftw_plan_dft_1d" yields proper result as I checked it with Matlab. The Real to Complex plan "fftw_plan_dft_r2c_1d" makes some zeroes at the end. I don't understand why.
Here is the simple testing code using both plans.
#include <iostream>
#include <complex.h>
#include <fftw3.h>
using namespace std;
int main()
{
fftw_complex *in, *out, *out2;
double array[] = {1.0,2.0,3.0,4.0,5.0,6.0,0.0,0.0};
fftw_plan p, p2;
int N = 8;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
out2 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
for (int i = 0; i < N; i++) {
in[i] = i+1+0*I;
}
in[6] = 0+0*I;
in[7] = 0+0*I;
cout << "complex array" << endl;
for (int i = 0; i < N; i++) {
cout << "[" << i << "]: " << creal(in[i]) << " + " << cimag(in[i]) << "i" << endl;
}
cout << endl;
cout << "real array" << endl;
for (int i = 0; i < N; i++) {
cout << "[" << i << "]: " << array[i] << endl;
}
cout << endl;
p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
p2 = fftw_plan_dft_r2c_1d(N, array, out2, FFTW_ESTIMATE);
fftw_execute(p); /* repeat as needed */
fftw_execute(p2);
cout << "fftw_plan_dft_1d:" << endl;
for (int i = 0; i < N; i++) {
cout << "[" << i << "]: " << creal(out[i]) << " + " << cimag(out[i]) << "i" << endl;
}
cout << endl;
cout << "fftw_plan_dft_r2c_1d:" << endl;
for (int i = 0; i < N; i++) {
cout << "[" << i << "]: " << creal(out2[i]) << " + " << cimag(out2[i]) << "i" << endl;
}
cout << endl;
fftw_destroy_plan(p);
fftw_destroy_plan(p2);
fftw_free(in);
fftw_free(out);
fftw_free(out2);
return 0;
}
Result:
complex array
[0]: 1 + 0i
[1]: 2 + 0i
[2]: 3 + 0i
[3]: 4 + 0i
[4]: 5 + 0i
[5]: 6 + 0i
[6]: 0 + 0i
[7]: 0 + 0i
real array
[0]: 1
[1]: 2
[2]: 3
[3]: 4
[4]: 5
[5]: 6
[6]: 0
[7]: 0
fftw_plan_dft_1d:
[0]: 21 + 0i
[1]: -9.65685 + -3i
[2]: 3 + -4i
[3]: 1.65685 + 3i
[4]: -3 + 0i
[5]: 1.65685 + -3i
[6]: 3 + 4i
[7]: -9.65685 + 3i
fftw_plan_dft_r2c_1d:
[0]: 21 + 0i
[1]: -9.65685 + -3i
[2]: 3 + -4i
[3]: 1.65685 + 3i
[4]: -3 + 0i
[5]: 0 + 0i
[6]: 0 + 0i
[7]: 0 + 0i
As you can see there is this strange difference between both plans and the result should be the same.
As you have noted, the fftw_plan_dft_1d function computes the standard FFT Yk of the complex input sequence Xn defined as
where j=sqrt(-1), for all values k=0,...,N-1 (thus generating N complex outputs in the array out), .
You may notice that since the input happens to be real, the output exhibits Hermitian symmetry, that is for N=8:
out[4] == conj(out[4]); // the central one (out[4] for N=8) must be real
out[5] == conj(out[3]);
out[6] == conj(out[2]);
out[7] == conj(out[1]);
where conj is the usual complex conjugate operator.
Or course, when using fftw_plan_dft_1d FFTW doesn't know the input just happens to be real, and thus does not take advantage of the symmetry.
The fftw_plan_dft_r2c_1d on the other hand takes advantage of that symmetry, and as indicated in "What FFTW Really Computes" section for "1d real data" of FFTW's documentation (emphasis mine):
As a result of this symmetry, half of the output Y is redundant (being the complex conjugate of the other half), and so the 1d r2c transforms only output elements 0...n/2 of Y (n/2+1 complex numbers), where the division by 2 is rounded down.
Thus in your case with N=8, only N/2+1 == 5 complex values are filled in out2, leaving the remaining 3 unitilialized (those values just happened to be zeros before the call to fftw_plan_dft_r2c_1d, do not rely on them being set to 0). If needed, those other values could of course be obtained from symmetry with:
for (i = (N/2)+1; i<N; i++) {
out2[i] = conj(out2[N-i]);
}