C語言數(shù)組指針的小例子
更新時(shí)間:2013年07月01日 14:39:50 作者:
這篇文章介紹了,用c語言實(shí)現(xiàn)的一個(gè)數(shù)組指針的小例子,有需要的朋友可以參考一下
1、功能:輸入6個(gè)學(xué)生的5門課程成績,計(jì)算出每個(gè)學(xué)生的平均分和每門課程的平均分。
2、C語言實(shí)現(xiàn)代碼:(其實(shí)就是用二維數(shù)組來實(shí)現(xiàn)的,二維數(shù)組的引用傳遞使用數(shù)組指針來完成)
#include <stdio.h>
#define STUDENT 5
#define SCORE 6
void input_array(float (*score)[STUDENT]);
void avg_score(float (*score)[STUDENT]);
void avg_course(float (*score)[STUDENT]);
/**
* calculate student average score and course average socore.
*/
int main(){
float a[SCORE][STUDENT];
input_array(a);
avg_course(a);
avg_score(a);
}
void input_array(float (*score)[STUDENT]){
int i, j;
for(i=0; i<SCORE; i++){
printf("input the %d student score:", i+1);
for(j=0; j<STUDENT; j++){
scanf("%f", score[i] + j);
}
}
}
void avg_course(float (*score)[STUDENT]){
int i,j;
float s;
for(j=0; j<STUDENT; j++){
printf("course%d ", j);
}
printf("\n");
for(i=0; i<SCORE; i++){
s=0;
for(j=0; j<STUDENT; j++){
printf("%f ", *(score[i] + j));
s += *(score[i] + j);
}
printf("\ts=%f, avg=%f\n" , s,s/STUDENT);
}
}
void avg_score(float (*score)[STUDENT]){
int i,j;
float s;
for(i=0; i<STUDENT; i++){
s = 0;
for(j=0; j<SCORE;j++){
s+= *(score[j]+i);
}
printf("%f " , s/SCORE);
}
}
2、C語言實(shí)現(xiàn)代碼:(其實(shí)就是用二維數(shù)組來實(shí)現(xiàn)的,二維數(shù)組的引用傳遞使用數(shù)組指針來完成)
復(fù)制代碼 代碼如下:
#include <stdio.h>
#define STUDENT 5
#define SCORE 6
void input_array(float (*score)[STUDENT]);
void avg_score(float (*score)[STUDENT]);
void avg_course(float (*score)[STUDENT]);
/**
* calculate student average score and course average socore.
*/
int main(){
float a[SCORE][STUDENT];
input_array(a);
avg_course(a);
avg_score(a);
}
void input_array(float (*score)[STUDENT]){
int i, j;
for(i=0; i<SCORE; i++){
printf("input the %d student score:", i+1);
for(j=0; j<STUDENT; j++){
scanf("%f", score[i] + j);
}
}
}
void avg_course(float (*score)[STUDENT]){
int i,j;
float s;
for(j=0; j<STUDENT; j++){
printf("course%d ", j);
}
printf("\n");
for(i=0; i<SCORE; i++){
s=0;
for(j=0; j<STUDENT; j++){
printf("%f ", *(score[i] + j));
s += *(score[i] + j);
}
printf("\ts=%f, avg=%f\n" , s,s/STUDENT);
}
}
void avg_score(float (*score)[STUDENT]){
int i,j;
float s;
for(i=0; i<STUDENT; i++){
s = 0;
for(j=0; j<SCORE;j++){
s+= *(score[j]+i);
}
printf("%f " , s/SCORE);
}
}
相關(guān)文章
C++使用expected實(shí)現(xiàn)優(yōu)雅的錯(cuò)誤處理
C++ 中提供了很多中方式進(jìn)行錯(cuò)誤處理。無論是通過拋異常還是通過錯(cuò)誤碼,標(biāo)準(zhǔn)庫都提供相應(yīng)的調(diào)用,今天本文為大家介紹的是使用expected進(jìn)行錯(cuò)誤處理,感興趣的可以了解一下2023-06-06
C++中回調(diào)函數(shù)(CallBack)的用法分析
這篇文章主要介紹了C++中回調(diào)函數(shù)(CallBack)的用法,較為詳細(xì)的分析了C++中回調(diào)函數(shù)(CallBack)的原理并以實(shí)例形式總結(jié)了其具體用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
pybind11: C++ 工程提供 Python 接口的實(shí)例代碼
這篇文章主要介紹了pybind11: C++ 工程如何提供 Python 接口,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
記逆向小白的第一次vbsedit 9爆破及內(nèi)存補(bǔ)丁制作過程
這篇文章主要介紹了記逆向小白的第一次vbsedit 9爆破及內(nèi)存補(bǔ)丁制作過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
C++ stack與queue模擬實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于c++stack與queue模擬實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
C語言之棧和堆(Stack && Heap)的優(yōu)缺點(diǎn)及其使用區(qū)別
本篇文章主要介紹了什么是棧(Stack) 、什么是堆( Heap),以及棧和堆的優(yōu)缺點(diǎn),同時(shí)介紹了應(yīng)該什么時(shí)候使用堆和棧,有需要的朋友可以參考下2015-07-07
C++使用printf語句實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換的示例代碼
在C語言中,printf 函數(shù)可以直接實(shí)現(xiàn)部分進(jìn)制轉(zhuǎn)換功能,通過格式說明符(format specifier)快速輸出不同進(jìn)制的數(shù)值,下面給大家分享C++使用printf語句實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換的示例代碼,感興趣的朋友一起看看吧2025-04-04

