• ベストアンサー

プログラムの問題

プログラムの課題ですがどうしてもわからないので質問させていただきます。 これが問題です Write a program that reads integers from the keyboard, creates a dynamic table that can store a ragged array and place the data in a dynamic table and then determine the minimum of each row of data. The design is following: The program starts by asking the user for the number of rows that must be stored. After allocating the row pointers (using calloc), the program asks for the number of entries in each row. The table is then filled with dtat supplied by the user from the keyboard. To demonstrate the application that could beused with this type of structure, we then determine the minimum of each row of data. The complete set of programs to build and fill the table is shown below. ここからプログラム #include <stdio.h> #include <stdlib.h> #include <limits.h> //Function declarations int** buildTable (void); void fillTable (int** table); void processTable (int** table); int smaller (int first, int second); int larger (int first, int second); int rowMinimum (int* rowPtr); int main(void) { //Local Declarations int** table; //Statements table = buildTable(); fillTable(table); processTable(); return 0; } In function rowMinimum use //Local declaration int rowMin = INT_MAX; int** table また、こちらのプログラムも使用します。 table = (int**)calloc (rowNum +1, sizeof(int&*)); table[0] = (int*)calloc (4, sizeof(int)); table[1] = (int*)calloc (7, sizeof(int)); table[2] = (int*)calloc (1, sizeof(int)); table[3] = (int*)calloc (3, sizeof(int)); table[4] = (int*)calloc (2, sizeof(int)); table[5] = NULL; まだC初心者でポインタなどあまりよく把握していません。丸投げになってしまいますがお願いします。

  • untn
  • お礼率0% (0/1)

みんなが選んだベストアンサー

  • ベストアンサー
  • f272
  • ベストアンサー率46% (8017/17135)
回答No.1

>The complete set of programs to build and fill the table is shown below. と書いてあるんだから、ここには書いてないようだけど、実はプログラムは全てあるんじゃないの? どの部分が理解できないのでしょうか?分からないところだけに質問を限定してください。 それにしても void processTable (int** table); と宣言しているのにmain関数では processTable(); としているのはどうして? それから table = (int**)calloc (rowNum +1, sizeof(int&*)); のsizeofの使い方はどこで習ったの? そして table[0] = (int*)calloc (4, sizeof(int)); のようにcallocの第1引数を固定の数値にして汎用性をなくすのは、どういう意図があるの?

関連するQ&A

  • cではよくてc++ではダメな理由

    #include <stdio.h> #include <stdlib.h> int main(void) { int *x; x = calloc(1, sizeof(int)); if (x == NULL) puts("記憶域の確保に失敗しました。"); else{ *x = 57; printf("*x = %d\n", *x); free(x); } return 0; } 上のソースなのですがcallocの戻り値がvoidなのでintにはキャストしてくれみたいなことを コンパイラに言われます。 cではコンパイルできてc++ではコンパイルできないのはなぜでしょう。

  • Unix

    GNOME端末のエラーについての質問です。 自分はUnix系のGNOME端末を使ってプログラムのコンパイルなどやってます。 今C言語で以下のようなプログラムをコンパイルしたところ、12行目でc:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘OpenHashTableAlloc’とエラーが出ました。このエラーの意味を教えていただけますでしょうか? #include <stdio.h> #include <stdlib.h> struct OpenHashTable{ int bucket_num; char *buckets; }; OpenHashTable OpenHashTableAlloc(void) ←12行目 { OpenHashTable *table; table = (OpenHashTable *)malloc(sizeof(OpenHashTable)); if(table == NULL) { return (NULL); } table->bucket_num = 0; table->buckets = NULL; return (table); } int main(void) { OpenHashTableAlloc(); return 0; }

  • テーブル配列の中身、出力値

    お世話になっております。 C言語勉強中のものです。 一定のひもをランダムに切断し、その長さを測ろうと思っています。 前回アドバイスを頂き、ソートを使って書いてみたのですが、詰まる部分があったのでご指導いただきたいと思いました。 #include <stdlib.h> #include <string.h> #include <stdio.h> #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))   int GetRandom(int min, int max);   int compare_int(const void* a, const void* b);   int compare_str(const void* a, const void* b); int main(void) {   int int_table[] = {GetRandom(0,10000)};   int i;   qsort( int_table, ARRAY_SIZE(int_table), sizeof(int), compare_int );   for( i = 0; i < ARRAY_SIZE(int_table); ++i ) {     printf("%d\n",int_table[i] ); }   return 0; } int compare_int(const void* a, const void* b) {   return ( *(int*)a - *(int*)b ); } int GetRandom(int min, int max) {     return min + (int)(rand()*(max-min+1.0)/(1.0+RAND_MAX)); } 【困っている箇所】 ・int int_table[]={} の中身に次のようなものを入れることで、ランダムに切ったひもを、ソートできると考えているのですが、その方法を色々ためしましたが上手くいきませんでした。このようなやり方で良いのか、その場合どのようにしたら良いか、また違うのかを教えていただきたいです。 for(i=0;i<10;i++) { printf("%d\n",GetRandom(0,10000)); } ・また最終的にソートしたものを比較させようと思っています。 「出力値を保存する方法」が分からないので教えて頂きたいです。 ----前回ご覧になってくださった方へ---- アドバイスありがとうございました。 まだ初心者ですが、色々ご教授下さるとうれしい限りです。

  • 多元配列について(ANSI C)

    動的多元配列で、 Voidポインタに、多種の型がぶら下がった多元配列を作り、 読み書きをしたいのですがどのようにしたらよろしいでしょうか。 具体的には、 x[0][1]は、intで「2」が入っている x[0][4]は、intで「9」が入っている x[1][2]は、charでしかも文字列の配列で「goo」が入っている x[1][5]は、charでしかも文字列の配列で「教えて」が入っている x[0]は、int型の配列。X[1]は、文字列型の配列。 というようなものです。 一応ソースは作ってみたのですが、int型では問題なくいくのですが、 文字列は、コンパイルはできますが、実行すると予期せぬことが起きます。 #include <stdio.h> #include <stdlib.h> #include <string.h> int main (void) {      void **topPointa;      int * iDataInput;      int * iDataOutput ;      char * chDataInput;      char * chDataInput2;      char * chDataOutput1 ;      // ポインタアドレス用のメモリ確保      topPointa = (void *) calloc (10 , sizeof(void *));      if(topPointa == NULL) {           printf("メモリが確保できません\n");           exit(-1);      }      //int配列のメモリ確保      iDataInput = (int * ) calloc (10 , sizeof(int));      if(iDataInput == NULL) {           printf("メモリが確保できません\n");           exit(-1);      }      iDataInput[0] = 3 ;      iDataInput[1] = 4 ;      topPointa[0] = (void * ) &iDataInput;      //int配列の取り出し      iDataOutput = *(int *) topPointa[0];      printf( "int: %d\n", iDataOutput[0] );      printf( "int: %d\n", iDataOutput[1] );      //char配列 のメモリ確保      chDataInput = (char * ) calloc (10 , sizeof(char *));      if(chDataInput == NULL) {           printf("メモリが確保できません\n");           exit(-1);      }      chDataInput2 = (char * ) calloc (10 , sizeof(char));      if(chDataInput2 == NULL) {           printf("メモリが確保できません\n");           exit(-1);       }      strcpy(chDataInput2 , "hoe");      chDataInput[0] = &chDataInput2;      topPointa[1] = (void * ) &chDataInput;      //char配列の取り出し      chDataOutput1 = *(char *) topPointa[1];      printf( "char: %S\n", chDataOutput1[0] );      free(iDataInput);      free(chDataInput);      free(chDataInput2);      return 0; } 言語は、C言語ANCI Cでお願いします。 以上。よろしくお願いします。

  • 配列を使って複数の線分を描くプログラムについて

    配列を使って複数の線分を描くプログラムについて、途中までは完成させたのですが、どうしてもわからない箇所があります。今日中に誰か回答をお願いします。 #include <stdlib.h> #include <stdlib.h> #include <stdio.h> #include <math.h> #include "inc2D.h" int *pxs,*pys,*pxe,*pye; int N; void draw_line(int xs,int ys,int xe,int ye) { // [この中は完成させました] } void polygon(N,xs,ys,xe,ye) int N; int xs[],ys[],xe[],ye[]; { /*  ここがどうしてもわかりません。for文を使った繰り返しではないかと考えて、色々試してみましたが、  エラーが出たり、表示されなかったりしてどうしても進みません。 */ void draw_line(); } void display(void) { void polygon(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); polygon(N,pxs,pys,pxe,pye); } int main(int argc, char *argv[]) { int i; glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH); glutCreateWindow("01kc999 Tsuyoshi SAITOH"); glutReshapeWindow(wsize,wsize); glutDisplayFunc(display); glutReshapeFunc(reshape); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glClearColor(0.0,0.3,0.3,0.0); scanf("%d",&N); pxs=(int *)malloc(sizeof(int)*N); pys=(int *)malloc(sizeof(int)*N); pxe=(int *)malloc(sizeof(int)*N); pye=(int *)malloc(sizeof(int)*N); for(i=0;i<N;i++) {scanf("%d %d %d %d",&(pxs[i]),&(pys[i]),&(pxe[i]),&(pye[i]));} glutMainLoop(); return 0; }

  • qsortの引数について

    以下のプログラムがあります。 int compare( const char **name1, const char **name2 ) { return strcmp( *name1, *name2 ); } int main( void ) { char *names[] = { "rand", "calloc", "malloc" }; int num = sizeof names / sizeof names[0]; qsort( names, num, sizeof( names[0] ), (int (*)(const void *, const void * ))compare ); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~この部分 return 0; } 上の「~~~」の上の部分のqsort関数の第4引数のキャストの意味が 分かりません。なぜ関数の前に引数が書かれるのでしょうか? またintの後にある(*)は「int型のポインタ」と言う意味なのか、 compare関数のポインタなのかも分かりません。 ご回答よろしくおねがいします。

  • C言語のポインタ

    あまり意識せずにポインタを使っているせいか,次のプログラムではまってしまいました. #include<stdio.h> #include<stdlib.h> int main(void) {  int *p, q;  p = (int *)malloc(sizeof(int));  q = (int *)malloc(sizeof(int));  *p = 2;  printf("%d\n", *p);  return 0; } コンパイルエラーで実行ファイルが出力されません. このプログラムで変数qはなぜポインタじゃないのでしょうか? 次にtypedefでptr_intという型を定義したプログラムは, 上のようなエラーが出力されず,期待とおりの結果になりました. #include<stdio.h> #include<stdlib.h> typedef int* ptr_int; int main(void) {  ptr_int p, q;  p = (int *)malloc(sizeof(int));  q = (int *)malloc(sizeof(int));  *p = 2;  *q = 3;  printf("%d\n", *p);  printf("%d\n", *q); return 0; } typedefすることでなぜエラーを回避することができるのでしょうか? よろしくおねがいします.

  • プログラムの動作

    独学でプログラムの勉強をしようと、サイトや本を見ながら勉強しているのですが、ポインタの使用がいまいち理解できません。 下にあるプログラムの動作の流れを順を追って教えていただきたいです。 (細かくいいますと、ポインタの動きと関数の動きの点を重点的に教えていただきたいです。(特にfor文内) 『以下プログラム』 1: #include<stdio.h> 2: 3: void h(char **p){ 4:     (*p)++; 5: } 6: 7: int main(void){ 8: 9: int i; 10: char str[]="abcdefghij"; 11: char *p_str; 12: 13: p_str=str; 14: for(i=0;i<(sizeof(str)/sizeof(str[0]));i++){ 15: printf("[%s]\n",p_str); 16: h(&p_str); 17: } 18: 19: return(0); 20: } 出力結果 [abcdefghij] [bcdefghij] [cdefghij] [defghij] [efghij] [fghij] [ghij] [hij] [ij] [j] []

  • CygwinでSTLの勉強をしていますが・・・

    今C++のSTLの勉強をしています。 本に載っているサンプルプログラムを打って実行しようとしたら エラーがでてしまいました。 エラーの内容はprintとtotalが見つかりませんというエラーです。 コンパイラはcygwinを使ってます。 よろしくお願いします。 /*for_each()アルゴリズム*/ #include<iostream> #include<algorithm> #include<vector> #include<functional> #include<> using namespace std; int main() { int n[]={100,200,300,400,500,600}; int size=sizeof n/sizeof(int),i; vector<int> v; for(i=0;i<size;++i) v.push_back(n[i]); for_each(v.begin(),v.end(),print<int>()); cout<<endl; cout<<(for_each(v.begin(),v.end(),total<int>())).gettotal()<<endl; return 0; }

  • プログラム(C)

    #include <stdio.h> #include <stdlib.h> #define FNAME "smp.bmp" #define WSIZE 256 #define HSIZE 256 #define BSIZE 1024 int main(void) { struct BMPFILEHEADER { ・   ・ }; struct BMPINFOHEADER { ・   ・ }; unsigned char img[HSIZE][WSIZE][3]; unsigned char buf[BSIZE]; struct BMPFILEHEADER lpHead; struct BMPINFOHEADER lpInfo; FILE *fp; int i; int j; int k; fp = fopen(FNAME,"rb"); if (fp==NULL) { printf("ファイルをオープンできません\n"); return 0; } fread(&lpHead.bfType, sizeof(unsigned short),1,fp); fread(&lpHead.bfSize, sizeof(unsigned int),1,fp); fread(&lpHead.bfReserved1, sizeof(unsigned short),1,fp); fread(&lpHead.bfReserved2, sizeof(unsigned short),1,fp); fread(&lpHead.bf0ffBits, sizeof(unsigned int),1,fp); fread(&lpInfo, sizeof(struct BMPINFOHEADER),1,fp); for(i=0;i<HSIZE;i++) { fread(buf,sizeof(unsigned char),WSIZE*3,fp); for(j=0;j<WSIZE;j++) { for(k=0;k<3;k++) { img[HSIZE-1-i][j][k]=buf[j*3+k]; } } } fclose; return 0; } このプログラムはBMP画像を読み込むプログラムなんですが このプログラムに画素値を出力するプログラムにしたいのですがうまくできません。 結果は(真っ白な画像の時)255255255・・・255255と出力したいのです。白黒画像なのでR=G=Bで1画素値は255だけでいいのですが。どうしても255255255や25500などとでてしまいます。 アドバイスお願いします。(800字までなので構造体の宣言は抜いてしましました) 。