Home / โจทย์จากเพื่อน
Page options

โจทย์จากเพื่อน


1. Enum  เขียนโปรแกรมให้ได้ผลลัพธ์ตามที่กำหนด

ผลลัพธ์

 

2. Struct

#include<stdio.h>
 void main(void)
 {
  struct{
   int a,b;
   char ch;
   } old;
  old.a = 500;
  old.b = 25;
  old.ch = 'T';
  f1(old);
 }
 f1(new)
 struct{
  int  x, y;
  char ch;
  } new;
 {
  printf ("%d \n",new.x);
  printf ("%d \n",new.y);
  printf ("%d \n",new.ch); 
 }

ผลลัพธ์ คือ 500 กับ 25

 

3. Variable length

เขียนโปรแกรมต่อไปนี้ ให้ได้ผลลัพท์เป็น -1


#include <stdio.h>
  #include <stdarg.h>
  void printargs(int b,...)
  {
    va_list s;
    int g;
    va_start(s,b);

    for(?)

    printf("%d",g);
    va_end(s);
    putchar('\n');
  }
  int main(void)
  {
    printargs(5,2,14,84,97,15,24,48,-1);
    printargs(84,51,-1);
    printargs(-1);
    printargs(1,-1);
    return 0;
  }

คำตอบ

#include <stdio.h>
#include <stdarg.h>
void printargs(int arg1, ...)
{
  va_list s;
  int g;
  va_start(s,arg1);
  for (g=arg1;g==-1;g=va_arg(s,int))
    printf("%d",g);
  va_end(s);
  putchar('\n');
}
int main(void)
{
  printargs(5,2,14,84,97,15,24,48,-1);
  printargs(84,51,-1);
  printargs(-1);
  printargs(1,-1);
  return 0;
}

 

4. จงเขียนโปรแกรมแสดงการหาผลบวกของเลขจำนวนเต็มบวก (1-10) โดยใช้ฟังก์ชั่นเรียกตัวเองชื่อ sum

ผลลัพธ์

 

5. FILE


    Post a comment

    Your Name or E-mail ID (mandatory)

     

    Note: Your comment will be published after approval of the owner.

    No Attachments Found. Click here to upload new file.




     RSS of this page

    Author: boombalaka   Version: 1.6   Last Edited By: Guest   Modified: 18 Feb 2008