How do I make structured array loop output what I want [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to input a number in each structure but I keep getting numbers I didn't even input.
#include <stdio.h>
#include <stdlib.h>
struct reg {
int course_num;
};
int main() {
struct reg arr_reg[2];
int i;
for (i = 0; i < 2; i++) {
printf("Enter course number: ");
scanf("%d"), &arr_reg[i].course_num;
}
for (i = 0; i < 2; i++) {
printf("course number: %d\n ", arr_reg[i].course_num);
}
}

A simple fix, move the , &arr_reg[i].course_num into the parenthesis after the closing quotation mark. Check your IDE's error flags whenever they pop up.

Simple mistake ,change "scanf("%d"), &arr_reg[i].course_num;"
to "scanf("%d",&arr_reg[i].course_num);"
#include <stdio.h>
#include <stdlib.h>
struct reg{
int course_num;
};
int main()
{
struct reg arr_reg[2];
int i;
for(i = 0; i < 2; i++)
{
printf("Enter course number: ");
scanf("%d",&arr_reg[i].course_num);
}
for(i = 0; i < 2; i++)
{
printf("course number: %d\n ", arr_reg[i].course_num);
}
return 0;
}

Related

Why does my C program not run even though it does not give an error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I have just started learning programming in c. I have written a program that has an arithmetic sequence based on the number of arithmetic sequence sentences n.
The program will not work after 17 without giving me an error
I have tried several different ideas but I have not received an answer
Thank you for your help.
#include <stdio.h>
int main()
{
int n ,k;
float d ;
printf("please write your arithmetic sequence sentences ");
scanf("%d",&n);
printf("\n");
printf("please write your common differences");
scanf("%d",&k);
printf("\n");
printf("please write your initial element ");
scanf("%f",&d);
printf("error 1");
printf("\n");
printf("error 2");
printf("number \t sum");
printf("erorr 3");
int i = 0;
int j = 0;
int sum = 0;
while (i < n)
j = d + i*k;
sum += j;
printf("%d\t%d",j,sum);
i++;
return 0;
}
it is not python. You need to use {} to declare compound statement
while (i < n)
{
j = d + i*k;
sum += j;
printf("%d\t%d\n",j,sum);
i++;
}

how can i copy string to another variable and output with if else statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
#include<stdio.h>
#include<string.h>
int main(void)
{
//Assigning string to name1//
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
//Assigning string to name2//
char name2[7] = "Mudus";
//prompt for input//
int i;
printf("Choose 1 or 0: ");
scanf("%i \n", &i);
}
//for copying string of name1 to copyname//
if (i = 0)
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
I want to copy the string to another variable and output with if-else condition through this way only but I am getting error.
The output is the expected identifier or ‘(’ before ‘if’
You probably want this:
#include <stdio.h>
#include <string.h>
int main(void)
{
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
char name2[7] = "Mudus";
int i;
printf("Choose 1 or 0: ");
scanf("%i", &i);
if (i == 0) // <<<<< use == instead of =
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
poinless comments removed
code snippets moved into main
code formatted properly
scanf("%i \n", &i); replaced with scanf("%i", &i);
if (i = 0) replaced with if (i == 0)

What should I do to get rid of this error message? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am running a hollow box statement and cant figure out the error message
error: expected identifier or ‘(’ before ‘{’ token" {
I have tried multiple variations of the bracket in different positions and it just causes more errors. Here is the updated code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
int i;
printf("Even numbers between 25 to 75: \n");
for (i = 25; i<=75; i++)
{
if(i%2 == 0)
{
printf("%d\t", i);
}
}
printf("All odd numbers between 500 to 400: \n");
for (i = 500; i>=400; i--)
{
if(i%2 == 0)
{
printf("%d\t", i-1);
}
}
int number, result, exponent;
result = 1;
printf("Enterthe base number: ");
scanf("%d", &number);
printf("Enter the exponent: ");
scanf("%d", &exponent);
while (exponent != 0)
{
result *= number;
--exponent;
}
printf("Answer = %d \n", result);
}
int f, w;
{
for (f = 1; f <= 7; f++)
{
for (w = 1; w <= 7; w++)
{
if (f==1 || f==7 || w==1 || w==7)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
This is an update to the first question since my first post didnt show the whole code and everyones answer was asking for it.
You should put an int main(void) before the body of your main function. You can then move your variables inside the function. After you've done this, the top of your code should look like:
int main(void) /* Here! */
{
int f, w; /* Move this inside the function. */
for (f = 1; f <= 7; f++)
...

Do not understand memory surfing in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#include <array.h>
int col, str;
int *point;
void setArr()
{
printf("Input columns="); scanf("%d", &col);
printf("Input strings="); scanf("%d", &str);
int num[str][col];
for(int i = 0; i < str; ++i)
{
for(int j = 0; j < col; ++j)
{
scanf("%d", &num[i][j];
}
}
point = num;
}
int main(void)
{
setArr();
printf("First=%d\n", *point);
printf("Number=%d", *point);
}
Output:
Input columns=2
Input strings=2
1
2
3
4
First=1
Number=1740639104
Here we have code in C, that have to get exact number from array using pointer, but during many attempts I understand that there is something I do not understand or just do not know.So there is a problem(or it have to be like this), namely, I refer to pointer ,which points on first element two times and I get different results in each case. Why it happened and in which way I could solve it? Thanks,everyone.
With
point = num;
you are setting point to an address of a function local variable. All further access of that will be undefined behaviour.

Why array doesn't print correct numbers? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Following code:
#include <stdio.h>
int izbaciSveProste(int n, int x[], int y[])
{
int i;
int flag=0;
for(i=2; i<n/2; i++)
{
if(n%i ==0)
{
flag =1;
break;
}
}
if(flag==1)
return 0;
else
return 1;
}
int main()
{
int i,j,n,x[100],y[100];
printf("Koliko elemenata zelite u polju?\n");
scanf("%d", &n);
printf("Enter elements in array:- ");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
int len = sizeof(x)/sizeof(x[0]);
for(i=0; i<len; i++)
{
if(izbaciSveProste(x[i]))
{
for(j=i; j<len; j++)
{
x[j] = x[j+1];
}
i--;
len--;
}
}
printf("Elementi nakon brisanja su:\n");
for(i=0; i<len; i++)
printf("%d\n",y[i]);
printf("\n");
return 0;
}
Purpose of this program should be to delete all prime numbers from array x[] with n elements, remaining elements should be rewritten in array y[] and show count of elements in array y[] in the end.I believe that function is okay and error is in main() specifically in storing y[].
Your function int izbaciSveProste(int n, int x[], int y[]) requires three arguments. Your code izbaciSveProste(x[i] passes one argument. That's not much enough. The compiler tells you that fact with the error message:
error: too few arguments to function 'izbaciSveProste'
Your function prototype has 3 parameters:
int izbaciSveProste(int n, int x[], int y[])
When you call the function, you only provide 1:
if(izbaciSveProste(x[i]))
The compiler wants to get all 3.
As your function doesn't even touch the 2 array parameters, you might simply remove them from the function definition and only take 1 integer.
Another problem:
You print y[i] in your loop but you never assign any value to that array.

Resources