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.
Related
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;
}
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 3 years ago.
Improve this question
Whats the meaning of [i] in the following example?
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
for (int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]); // HERE
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
i here is a variable.
In your code, [i] acts as the index of values and is used to access the element in array values.
Edit:
Since there is a //HERE comment in your code, im going to assume you would also want what [i] does there. The expression &value[i] basically gives the address of value[i] ,i.e, the "ith" element of the array.
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 5 years ago.
Improve this question
Can anyone tell what I am doing wrong here?
Problem statement:
https://practice.geeksforgeeks.org/problems/good-or-bad-string/0
My code:
#include <stdio.h>
#include<string.h>
int is_vowel(char a) {
if(a==97||a==101||a==105||a==111||a==117){
return(1);
}
return(0);
}
int main() {
//code
int t,i;
scanf("%d",&t);
for(i=0;i<t;i++){
char str[100];
scanf("%s",str);
printf("%s",str);
int c_cnsnt=0;
int c_vwl=0;
int g_b=1;//suppose good
for(int j=0;j<strlen(str);j++){
//("%c",str[j]);
int num=is_vowel(str[j]);
printf("Debug %c %d %d\n",str[j],num,strlen(str));
if(is_vowel(str[j])) {
c_vwl++;
}
else { c_cnsnt++;}
if(c_vwl==c_cnsnt){
c_cnsnt=0;
c_vwl=0;
}
else {
if(c_vwl>5||c_cnsnt>=3){
g_b=0;
break;
}
}
}
printf("%d\n",g_b);
}
return 0;
}
Sample
Input:
2
aeioup??
bcdaeiou??
Output:
1
0
My solution link:
https://code.hackerearth.com/9bca55K
Why does the for loop not work for the 2nd string?
Hint: You have to clear the the consonant and vowel counts after increment the other (e.g {c_vwl++;c_cnsnt=0;}), not when they are equal, and always tests your BAD condition.
I will not give you a sample code. Good luck
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'm a beginner and I am having a really hard time while doing this program.
The question is:
(1/1!)+(2/2!)+(3/3!)+(4/4!)- - - -n
So here are the n number of terms(in which a number is divided by its factorial) and I have to display the output of the sum of any number of terms which are given in scanf function.
Only one thing I know is that this program can be done by using "Nested for" loop but I haven't perfect grip yet on C language. So you guys have to have help me out in this. :)
#include <stdio.h>
#include <conio.h>
void main(void){
int s,a,b,n,fact=1;
//clrscr();
printf("Enter number of terms=");
scanf("%d",&n);
for(a=1;a<=n;a++) {
fact=fact*a;
b=(a/fact);
printf("Sum=%d",s);
}
getche();
}
P.S It's must for me to do it with "Nested for" loop.
No you do not need any Nested for loops to solve your problem. Here's a procedure you may follow:
function factorial
Input: numbers L.
Output: factorial of L.
function sum
Input: n.
Output: sum.
sum = 0;
for i = 1 to n, do
sum ← sum + (i / factorial(i))
return sum
#include <stdio.h>
int main(void) {
// your code goes here
int n;
float sum = 0,d,fact =1,j,i;
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++){
fact = 1;
for (j = 1; j <= i; j++){
fact = fact * j;
}
d = (float) i / (float) fact ;
sum = sum + d;
}
printf("sum = %f", sum);
return 0;
}
Its working ..you can check over here :-https://ideone.com/JVXQVX
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 8 years ago.
Improve this question
I can't finish this code please help me! I have to matrix, and the program reads items of matrix in function
int main(int argc, char *argv[])
{
int r1,c1;
printf("Enter dimension of first matrix :");
scanf("%d %d",&r1,&c1);
int m1[r1][c1];
GetArray(m1,r1,c1);
system("PAUSE");
return 0;
}
void GetArray(int arr[][],int _row,int _column)
{
int i,j,num;
printf("Enter number: ");
for(i==0;i<_row;i++){
for(j==0;j< _column;j++){
scanf("%d",&num);
arr[i][j]=num;}} //give error in this line
}
In C programming, == is used for comparison and = is used for assignment operations. You would definitely want to assign values to j and i in your for loops. In your case, you are not initializing the loop variables (when you declare them in the beginning of the function) and since they get garbage values when they are not initialized, you try to reach beyond the bounds of the array you are using in the for loops, thus getting a segmentation fault.
int main(int argc, char *argv[])
{
int r1,c1;
printf("Enter dimension of first matrix :");
scanf("%d %d",&r1,&c1);
int **m1;
for(int i = 0; i<r1 ;++i) // use c99 !
m1[i] = malloc(c1* sizeof(int));
GetArray(m1,r1,c1);
system("PAUSE");
for(int i = 0; i<r1 ;++i)
free(m1[i]);
return 0;
}
void GetArray(int ** m1,int _row,int _column)
{
int i,j,num;
printf("Enter number: ");
for(i=0;i<_row;i++){
for(j=0;j< _column;j++){
scanf("%d",&num);
m1[i][j]=num;}} //give error in this line
}
Untested