Stroustrup 5.9,练习10(使用功能) [英] Stroustrup 5.9, exercise 10 (using a function)

查看:54
本文介绍了Stroustrup 5.9,练习10(使用功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不起作用,我知道for循环中存在一些问题。

" print_arr"功能。我无法纠正我所获得的结果。

越来越多。我没有编译时错误,只是语义错误导致麻烦:


预期:1月,2月,3月....十二月

GOT:一月,一月,一月.........一月


------------ - 计划--------------

/ * Stroustrup,5.9,运动10

声明:

定义一个字符串数组,其中字符串包含名称months。

打印这些字符串。将数组传递给打印那些

字符串的函数。


解决方案:


1.)1,我将打印数组intmain使用for循环和数组

索引。


2.)然后我将使用函数打印数组并将数组传递给

作为参数的函数(通过引用传递)。


注意:发布的代码是执行(2)

* /


#include< iostream>


void print_arr(const char **,size_t);


int main()

{

const char * arr [] = {" january"," february"," march"," april"," may",br />
june,

7月,8月,9月,10月,

" 11月" ;,

" december"};


const size_t arr_size = sizeof(arr)/ sizeof(* arr);

print_arr(arr,arr_size);


返回0;


}


void print_arr(const char ** arr,size_t arr_size)

{

const char ** p = arr;


std :: cout<< " \ n \\\\\\\\\\\\\\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ cout<< * p<< std :: endl;


}


--------------输出----- -----------------

[arch @ voodo tc ++ pl] $ g ++ -ansi -pedantic -Wall -Wextra 5.9_ex-10- < br $> b $ b function.cpp

[arch @ voodo tc ++ pl] $ ./a.out


使用功能
january

january

january

january

january

1月

january

january

january

january

january

january

[arch @ voodo tc ++ pl] $

this does not work, i know there is some problem in the "for loop" of
"print_arr" function. i am not able to correct the weired results i am
getting. i have no compile time error, it is only semantic-bug that is
causing the trouble:

EXPECTED: january, february, march....december
GOT: january, january, january.........january

------------- PROGRAMME --------------
/* Stroustrup, 5.9, exercise 10

STATEMENT:
define an array of strings,where strings contains the names months .
Print those strings. Pass the array to a function that prints those
strings.

SOLUTION:

1.) 1st, i will print array int he "main" using "for" loop and array
indexing.

2.) then i wil print he array using a function and passing the array
to the function as argument(pass by reference).

NOTICE: posted code is implementation of (2)
*/

#include<iostream>

void print_arr(const char**, size_t);

int main()
{
const char* arr[] = {"january", "february", "march", "april", "may",
"june",
"july", "august", "september", "october",
"november",
"december"};

const size_t arr_size = sizeof(arr) / sizeof(*arr);

print_arr(arr, arr_size);

return 0;

}

void print_arr(const char** arr, size_t arr_size)
{
const char** p = arr;

std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i)
std::cout << *p << std::endl;

}

-------------- OUTPUT ----------------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra 5.9_ex-10-
function.cpp
[arch@voodo tc++pl]$ ./a.out

USING FUNCTION
january
january
january
january
january
january
january
january
january
january
january
january
[arch@voodo tc++pl]$

推荐答案

g ++ - ansi -pedantic -Wall -Wextra 5.9_ex-10-

function.cpp

[arch @ voodo tc ++ pl]
g++ -ansi -pedantic -Wall -Wextra 5.9_ex-10-
function.cpp
[arch@voodo tc++pl]


./ a.out


使用功能

january

january

january

january

january

january

january

january

1月

january

january

january

[arch @ voodo tc ++ pl]
./a.out

USING FUNCTION
january
january
january
january
january
january
january
january
january
january
january
january
[arch@voodo tc++pl]







这篇关于Stroustrup 5.9,练习10(使用功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆