c언어 질문이요

Aaqws의 이미지

int main(void)
{
	int i;
	int arr[100];
	double sum=0;
	int max;
	int min;
	int size;
 
	scanf("%d",&size);
 
	for(i=0;i<size;i++);
	{
		scanf("%d",&arr[i]);
		sum += arr[i];
	}
 
	for(max=arr[100], min=arr[100],i=1; i<=9;i++)
	{
		if(max<arr[i])
		{
			max=arr[i];
		}
		if(min>arr[i])
			min=arr[i];
	}
 
}

안녕하세요!처음 가입했습니다.일단 질문부터 하겠습니다.
초보라 잘 모르긴하는데... 구글을 뒤지면서 찾아가면서 해보긴했으나...
생각하는 대로 안되네요..
그 입력하는 숫자만큼 배열의 크기를 결정한다...랄까
배열에 100만큼 공간이 있는데 5를 입력하면 5만큼만 사용하는..
그리고 그 만큼 숫자를 받아서 평균 최대값 최소값 등등을 구하려 하는데
일단 숫자 받는것부터가 진행이 안되어서 뒤를 신경 쓸 새도 없네요..ㅠㅠ
이거 오늘안에 답해주실 수 있나요?ㅠㅠ

kjw4569의 이미지

이렇게 시작하나요? 그럼 scanf_s를 써야합니다.

ㅇㅇ의 이미지

for(max=arr[100], min=arr[100],i=1; i<=9;i++)
 
max=arr[0];
min=arr[0];
for(i=1;i<size;i++)

으로 해보시면 어떤가요?

익명 사용자의 이미지

평소라면 이런 엉터리 질문에 대답하지 않겠지만 명절 마지막 날이라(?) 대답해봅니다.
이 질문이 왜 엉터리인지 질문자께서는 좀 생각을 해보셔야 할 것 같습니다. 프로그래밍 수준과는 관계 없는 다른 문제입니다.
다른 한 편으로 어떤 교재를 가지고 공부하시길래 (또는 어떤 사람에게 배우길래) 2017년에 이런 오래된 스타일의 C 코드를 쓰는 지 답답하기도 하구요.

#include <stdio.h>
 
int main() {
	int size;
	scanf("%d", &size);
	int arr[size];
 
	for(int i = 0; i < size; i++) {
		scanf("%d", &arr[i]);
	}
 
	int sum = 0;
	int max = arr[0];
	int min = arr[0];
	for(int i = 0; i < size; i++) {
		sum += arr[i];
		if(max < arr[i]) {
			max = arr[i];
		}
		if(min > arr[i]) {
			min = arr[i];
		}
	}
 
 	printf("sum=%d, max=%d, min=%d\n", sum, max, min);
 
	return 0;
}

익명 사용자의 이미지

음... 불행히도 ms 컴파일러는 variable length array를 지원하지 않습니다. 아마도 이 코드를 컴파일하지 못할겁니다. 테스트는 안해봤습니다만.

익명 사용자의 이미지

저도 비주얼 스튜디오가 없어서 테스트를 해볼 수는 없는데 검색을 해보니 정말 그런 것 같군요. 그렇다면 혹시 질문자께서 비주얼 스튜디오를 사용하실 경우를 대비해서 답변을 다시 마무리해보겠습니다.

간단히

int arr[size];

int arr[100];

으로 바꿔주면 됩니다. 단, 입력 받은 size가 100보다 크면 다시 입력을 받거나 에러 메세지를 내고 종료하는 코드가 추가되어야겠지요. 이건 질문자께서 해보시기를.

다른 방법으로

#include <stdlib.h>
(중략)
int* arr = malloc(sizeof(int) * size);
(중략)
free(arr);
return 0;

와 같이 바꾸는 방법도 있습니다. malloc에 대해서는 역시 공부를 해보시면 되겠지요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.