Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm writing a program that calculates results of football matches.I'm trying to store the team ids' in an array with a length of 10 defined at the top but i keep getting getting a build error from the array. I realise the syntax might be wrong but how else can i use a variable to specify array length?
The error message i'm getting is : expected expression before '{ ' token.
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 10
int main() {
int numberofmatches, hometeamid, awayteamid, hometeamgoals, awayteamgoals;
int hometeamwins = 0;
int winratio;
int teamid[ARRAY_SIZE];
printf("Enter number of matches played \n");
scanf("%d", &numberofmatches);
if (numberofmatches > 0) {
int x = 0;
do {
printf("Enter match stats in order Home_team_ID,Away_Team_ID,Goals_Home,Goals_Away\n");
scanf("%d %d %d %d", &hometeamid, &awayteamid, &hometeamgoals, &awayteamgoals);
teamid[ARRAY_SIZE] = {hometeamid}; //Error is on this line
if (hometeamgoals > awayteamgoals) {
hometeamwins++;
}
x++;
}
while (x < numberofmatches);
winratio = hometeamwins / numberofmatches;
printf(" %d :teamidth %d :winratio", teamid[0], winratio);
}
return 0;
}
This
teamid[ARRAY_SIZE] = {hometeamid};
is the syntax for defining an array of size ARRAYSIZE and initialising it incompletely.
You try it in the middle of a loop.
In case you want to write to an array member you probably want
teamid[x] = hometeamid;
Also, I recommend making sure that you do not write beyond teamid[9], which is the last legal member of the array, for ARRAY_SIZE == 10.
Your defining an array of size 10, and then accessing it at index 10. Since arrays are indexed 0..n-1 you need to access index 9 to get the end of the array instead of 10.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
New programmer here, I have a few lines of code that I've finally managed to get to function but I'm running into a bit of a design issue. The code itself, at least to me, function as intended but I don't know how to get my terminal to display "Insert number here". This is how to code currently looks.
#include "cs50.h"
#include <stdio.h>
int main(void)
{
int n;
do
{
n = GetInt();
printf("Your number is!"/n);
}
while (n<1)
{
return n;
}
}
I'm looking for a way to get the same outcome as n = get_int ("Insert number: "); but that method brings up errors such as:
prompt.c:10:7: warning: implicit declaration of function 'get_int' is
invalid in C99 [-Wimplicit-function-declaration]
{ n = get_int("Insert number here %i\n", n);
or if I'm using n = GetInt("Insert number: "); I get this error message.
prompt.c:10:14: error: too many arguments to function call, expected 0,
have 2
{ n = GetInt("Insert number here %i\n", n);
~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:87:1: note: 'GetInt' declared here
int GetInt(void);
^
1 error generated.
Is there a way to get this to function so my terminal can prompt me with "Insert number here: ". Thank you and cheers!
If you want to print "Insert Number here:", you may use the prinf function.
#include <stdio.h>
int main()
{
int testInteger;
printf("Insert Number here: ");
scanf("%d", &testInteger);
printf("Your Number is %d",testInteger);
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Ive got a problem with this code, im trying to add up array1 with array2.
I enter the numbers for array2 by Command line parameters.
When i enter 10 numbers it is working but when i add less than 10 I get an Memory access error.
My question is now: how do i fill up the missing array fields with the number 0? For example : I enter 9 numbers and the 10th field should be 0.
You are not checking how many command line arguments are passed, and when you index into the command line argument array, you will get an out-of-bounds error.
In you addiren function, you should take advantage of the argc that is passed and used that in your for loop limit.
#include <stdio.h>
#include <stdlib.h>
int addiren(int argc, char**argv){
int array_one[10] = {0,1,1,2,3,5,8,13,21,35};
int array_two[10] = {0}; //Quick way to set the array to all zeros
int array_three[10] = {0};
//Set array_two with your cmd-line args, notice the use of argc
for(int i = 1; i<argc && i<=10; i++){
array_two[i-1] = atoi(argv[i]);
}
//Add array_one with array_two to array_three
for(int i = 0; i<10; i++){
array_three[i] = array_one[i]+array_two[i];
}
//Return an int since that's what the function return type requires
return 0;
}
Hope this helps!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm doing a project now with C language and i have a problem in one part on project.Basically im trying to find maximum M numbers in my main array and i'm taking their ID numbers to another array.The ID numbers of the these max numbers are their position in array.
My main array is tresh_arr and have N numbers,the numbers are double.
My code block which doesn't work is :
double max=0;
int *max_arr;
int k,tmp,flag=0;
max_arr=calloc(M,sizeof(int));
for(i=0;i<M;i++) //I will find maximum M numbers
{
for(j=0;j<N;j++) //There is a N numbers in my array
{
if( *(tresh_arr+j) > max ) //I'm trying to take the Id numbers to the tresh_arr
{
flag=0;
for(k=0;k<i+1;k++)
{
if( *(max_arr+k)==j )
{
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
{
max = *(tresh_arr+j);
tmp=j;
}
}
}
*(max_arr+i)=tmp;
}`
But my code doesn't work and just finding maximum number and writing M times the Id of this maximum number.What should i do? Thank you and sorry for my faults if have.
Following are mistakes in your program:
1) In second last line you should do
*(max_arr+i)=max; // not tmp
2)Inside each forloop over i set max to least number
for(i=0;i<M;i++) //I will find maximum M numbers
{
max =-1; //**********YOU MISSED THIS*****************
for(j=0;j<N;j++) //There is a N numbers in my array
{
3) Above break you should set flag to 1 and no need to set it to 0 again and in if you should check for flag==0 which basically means element was not found
for(k=0;k<i;k++)
{
if( *(max_arr+k)==tresh_arr[j] )
{
flag=1;
break;
}
}
if(flag==0)
{
max = *(tresh_arr+j);
tmp=j;
}
Also its good to write *(A+j) as A[j] in C, they both are exactly same, earlier one is sort of too verbose
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I keep getting the output of 100 when I do what everyone else online has been posting about:
int total = sizeof(num)/sizeof(int);
It doesn't seem to work for me. Here's my code:
int main() {
static int num[100];
int totalNum = sizeof(num)/sizeof(int);
return 0;
}
void readNumbers(int* num) {
int i = 0;
FILE *fp;
fp = fopen("/Users/Documents/hello.txt", "r");
if (fp == NULL) {
printf("Could not load file");
exit(0);
}
/* Loads numbers into num array */
int number;
while(fscanf(fp, "%d", &number) > 0) {
num[i] = number;
i++;
}
}
My output is 100 so I'm assuming there isn't anything that is inserted into the num array? And if I print out sizeof(num) it gives me a hundred; 4 bytes * 100 = 400.
Here is what is in hello.txt:
14 21 39 48 109 3882
Unlike arrays in other languages (that can grow and shrink, and will tell you how many elements the array currently contains), a C array is just a simple block of memory that has a fixed size. You declared an array that can hold 100 elements, and that's all sizeof is going to tell you.
If you want to know how many numbers you've put in the array, then you have to keep track of that in a separate variable. The array itself doesn't contain that information.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am beginner in C. I want to make an array whose size will be taken from user from scanf function because an array may have any size which is not known when program starts.
And how can I input array element as well:
I will like to have my program output as:
Please enter the number of array element: 4
Enter the elements: 12 43 5 6
The elements you entered are: 12 43 5 6
Is it possible to do this? How can I make my output like this?
Yes it is very possible. It is called dynamic memory allocation.
What you would do is create a pointer and then allocate it
later.
int *array;
int num_elements;
//Get number of elements here
array = (int *)malloc(sizeof(int) * num_elements);
if(!array){ //Good practice to check if the allocation worked
printf("Allocating %d bytes failed\n", (int)sizeof(int) * num_elements);
return -1;
}
//Use the array are normal
free(array); // don't forget to free allocated memory
Pointer access works just like a static array i.e. array[0] = whatever
edit:
Don't forget you should include stdlib.h when using malloc()
Dynamic memory work well to your purpose, however as mention earlier by Gopi, C99 allow you to directly use the stack.
Here is another solution using stack instead of heap memory:
#include <stdio.h>
int main(void)
{
int nb;
scanf("%d", &nb);
printf("%d\n", nb);
// declaration
char my_tab[nb];
if (nb > 2)
{
// I use my table as I want...
my_tab[0] = 'a';
my_tab[1] = 0;
printf("%s\n", my_tab);
}
return (0);
}
I hope this will help you understand better, the different kind of memory allocation.
A simple way of doing this with only basic knowledge might be to set the user input as a variable and then use that when you describe the size of the array:
#include <stdio.h>
int main(void)
{
int arraysize, i;
printf("Please input an array size.\n");
scanf("%d", &arraysize); //Will set the user input and use it as array size
getchar(); //Has user hit enter to continue
i = arraysize; //This is for sheer convenience in the for() loop
int array[i]; //This creates an array of the size inputted by the user
printf("Please input %d numbers to fill the array.\n", i);
for(i = 0; i<arraysize; i++) //Has the user put in a number for every space in the array
{
scanf("%d", &array[i]); //The i coordinate updates with the i++
getchar();
}
printf("Your numbers were: \n");
for(i = 0; i<arraysize; i++) //Same thing as the previous for() loop
{ //except we are printing the numbers in the table
printf("| %d |", array[i]);
}
}
The output looks like:
[PROGRAM BEGINS]
Please input an array size.
5
Please input 5 numbers to fill the array.
1 2 33 4 5
Your numbers were:
| 1 || 2 || 33 || 4 || 5 |
[PROGRAM ENDS]
Hope that helps!