(c언어 프로그래밍) 죄수의 딜레마 소스코드좀 수정해주세요ㅜㅡㅠㅠㅡㅜ<code>

chawon0406의 이미지

제가 만드려는 프로그램이 죄수의 딜레마에 관한 건데,
a와 b라는 사람이 각각 자백하는 것을 0, 협조하는 것(침묵) 1이라고 놓고 무작위로 프로그램이 100번 시뮬레이팅해서
a의 수감형량의 평균을 구하는건데 오류가 6개 있어요ㅜㅠㅜ 제가 너무 못해가지고 이거 구동되게 고쳐주시는분 있으시면 평생 감사히 살게요!

이렇게 떠요))
prog.c : 'main'함수에서 :
prog.c : 7 : 10 : warning : 함수의 암시적인 선언 'rand'[-Wimplicit-function-declaration]
int a = rand () % 2;
^ ~~~
prog.c : 14 : 7 : 오류 : 배열 'arr'의 크기가 정수가 아닌 형식입니다.
int arr [& i] = 5;
^ ~~
prog.c : 14 : 3 : 오류 : 가변 크기의 객체를 초기화 할 수 없습니다.
int arr [& i] = 5;
^ ~~
prog.c : 17 : 7 : 경고 : 사용되지 않는 변수 'b'[-Wunused-variable]
int b = rand () % 2;
^
prog.c : 16 : 7 : 경고 : 사용되지 않는 변수 'a'[-Wunused-variable]
int a = rand () % 2;
^
prog.c : 14 : 7 : 경고 : 사용되지 않는 변수 'arr'[-Wunused-variable]
int arr [& i] = 5;
^ ~~
prog.c : 20 : 3 : 오류 : 가변 크기 개체가 초기화되지 않을 수 있습니다.
int arr [i] = 0;
^ ~~
prog.c : 23 : 7 : 경고 : 사용되지 않는 변수 'b'[-Wunused-variable]
int b = rand () % 2;
^
prog.c : 22 : 7 : 경고 : 사용하지 않는 변수 'a'[-Wunused-variable]
int a = rand () % 2;
^
prog.c : 20 : 7 : 경고 : 사용되지 않는 변수 'arr'[-Wunused-variable]
int arr [i] = 0;
^ ~~
prog.c : 26 : 3 : 오류 : 가변 크기 개체가 초기화되지 않을 수 있습니다.
int arr [i] = 10;
^ ~~
prog.c : 29 : 7 : 경고 : 사용되지 않는 변수 'b'[-Wunused-variable]
int b = rand () % 2;
^
prog.c : 28 : 7 : 경고 : 사용하지 않는 변수 'a'[-Wunused-variable]
int a = rand () % 2;
^
prog.c : 26 : 7 : 경고 : 사용되지 않는 변수 'arr'[-Wunused-variable]
int arr [i] = 10;
^ ~~
prog.c : 32 : 3 : 오류 : 가변 크기의 객체를 초기화 할 수 없습니다.
int arr [i] = 2;
^ ~~
prog.c : 35 : 7 : 경고 : 사용되지 않는 변수 'b'[-Wunused-variable]
int b = rand () % 2;
^
prog.c : 34 : 7 : 경고 : 사용되지 않는 변수 'a'[-Wunused-variable]
int a = rand () % 2;
^
prog.c : 32 : 7 : 경고 : 사용되지 않는 변수 'arr'[-Wunused-variable]
int arr [i] = 2;
^ ~~
prog.c : 7 : 6 : 경고 : 변수 'a'가 설정되었지만 사용되지 않음 [-Wunused-but-set-variable]
int a = rand () % 2;
^

#include <stdio.h>
 
int main() {
 
	int i = 0;	//a의 수감일 계산 횟수 변수
	int arr[100];
	int a = rand() % 2;
	int b = rand() % 2;  //a.b 두 사람의 반응을 난수로 지정
	int sum = 0;	//배열의 합 변수 지정
	int k = 0;
 
	while(i < 100) {
	if(a=0, b=0) {	//둘 다 배신-> arr[i]의 값이 5
	 int arr[&i] = 5;
	 i++;
	 int a = rand() % 2;
	 int b = rand() % 2;
	}
	if(a=0, b=1) { //a석방-> arr[i]의 값이 0
	 int arr[i] = 0;
	 i++;
	 int a = rand() % 2;
	 int b = rand() % 2;
	}
	if(a=1, b=0) {	//b배신-> arr값이 10
	 int arr[i] = 10;
	 i++;
	 int a = rand() % 2;
	 int b = rand() % 2;
	}
	if(a=1, b=1) { //a,b배려-> arr값이 2
	 int arr[i] = 2;
	 i++;
	 int a = rand() % 2;
	 int b = rand() % 2;
	}
	}
	while(k < 100) {
	 sum += arr[k];
	 k++;
	}
 
	printf("a의 평균 구형일은 %d년", sum % 100);
	return 0;
}

lalupo20의 이미지

a=1은 a와 1이 같다가 아니라 a에 1값을 배정한다는 의미입니다. 원하시는 대로 고치려면 a==1이 되야 할 것이고
조건문에 ,를 쓰셨는데 조건1 조건2 이렇게 있을때 둘다 참이어야 하면 &&를 둘 중 하나만 참이면 되면 ||를 쓰셔야 합니다. 일단 눈에 보이는건 여기까지네요.

lalupo20의 이미지

int a = 1 이런식으로 한번 선언했으면
a = 2 이런식으로 사용만 하면 됩니다.

덜덜덜의 이미지

질문수준이 핑거 프린스네요

pi의 이미지

#include 도 앞에 넣으세요.

pi의 이미지

#include <math.h> 도 넣으세요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.