c++如何打印数组值相邻而不是在下面
C++ how to print out array values next to each other rather than underneath
我已经制作了这段代码,并希望它打印出来如下:
元素的值0 01 02 03 04 05 06 07 08 09 0 之前而不是在彼此的下面。谁能给我指个正确的方向吗?
#include <iostream> int main(){ int n[10] = {1,2,3,4,5,6,7,8,9,10}; int x[10] = {0,0,0,0,0,0,0,0,0,0}; std::cout << "Element" <<std::endl; for(int i = 0; i < 10; i++) { std::cout <<n[i] << std::endl; } std::cout << "Value" <<std::endl; for(int y = 0; y <10; y++) { std::cout << x[y] << std::endl; } std::cout << "size of array: " << sizeof(n) << std::endl; }
在一个循环中打印相邻的值,而不是使用两个循环(我更改了x
的内容,以确保我们看到发生了什么):
#include <iostream>
int main(){
int x[10] = {5,8,1,2,4,6,7,1,0,9};
std::cout << "Element Value" << std::endl;
for(int i = 0; i < 10; i++) {
std::cout << " " << i << " " << x[i] << std::endl;
}
std::cout << "size of array: " << sizeof(x) << std::endl;
return 0;
}
https://ideone.com/vAaLyl 输出:Element Value
0 5
1 8
2 1
3 2
4 4
5 6
6 7
7 1
8 0
9 9
size of array: 40
除此之外,不需要数组n
;使用您的索引作为索引
相关文章:
- 如何在动态数组上使用搜索函数
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 如何在向量数组中插入元素?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 在字符数组维度分析中构造字符数组
- 在对象数组中搜索字符串并返回相应值的函数
- 在 char* 数组中添加两个索引
- 如果我在字符数组上使用 close() 会发生什么?
- 按字母顺序打印存储在字符数组中的信息
- 在子数组中查找多个查询的不同(唯一)值的数量
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 在字符数组 (C++/根) 中查找双精度值
- 如何在新数组较小时创建新数组并将旧数组的最后一个元素复制到新数组中?
- 如何在运行时在对象数组中动态追加新对象C++并打印它们
- 在动态数组中添加/删除C++元素
- 在矢量数组C++中使用算法库操作
- 防止在字符数组中输入双倍空格在 C++