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 tried to run this code so many times, every time the output is just the same, I'm not getting, what's the use of such a code?
#include<stdio.h>
#include<conio.h>
void increment(int,int, int);
int main()
{
int a[20],i;
clrscr();
printf("Enter array elements");
for(i=0;i<20;i++)
scanf("%d", &a[i]);
increment(a[0],a[5],a[2]);
printf("After passing array:\n");
for(i=0;i<20;i++)
printf("%d\t", a[i]);
getch();
return 0;
}
void increment(int x, int y, int z)
{
//int i;
x++;
y++;
z++;
//for(i=0;i<size;i++)
// x[i]++;
}
To have any usefulness, the address of the values to be modified need to be passed to increment(), not the values themselves:
Change this:
void increment(int x, int y, int z)
{
//int i;
x++;
y++;
z++;
// for(i=0;i<size;i++)
// x[i]++;
}
To this:
void increment(int *x, int *y, int *z)
{
//int i;
(*x)++;
(*y)++;
(*z)++;
// for(i=0;i<size;i++)
// x[i]++;
}
Then change this:
increment(a[0],a[5],a[2]);
to this:
increment(&a[0],&a[5],&a[2]);
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 7 months ago.
Improve this question
#include <stdio.h>
int display(int arr[], int n);
int main()
{
int i, n;
printf("\nEnter the Size of Array:");
scanf("%d", &n);
int arr[n];
printf("\nEnter the %d Values of Arrays", n);
for (i = 0; i < n; i++)
{
scanf("%d", arr[i]);
}
display(&arr[0], n);
return 0;
}
int display(int arr[], int n)
{
int i;
printf("\nThe %d elements are:\n");
for (i = 0; i < n; i++)
{
printf("Array [%d]= %d\n", i, arr[i]);
}
}
This is the code for printing the array values using function. And in Run-time it gives Segmentation fault error. Help me to fix this.
You have to pass the address of the variable when calling scanf() in the loop, just as you do when reading n.
When passing an array to a function, we usually just write the name of the array. This is equivalent to passing &arr[0], but we don't normally write that out.
#include <stdio.h>
int display(int arr[], int n);
int main()
{
int i, n;
printf("\nEnter the Size of Array:");
scanf("%d", &n);
int arr[n];
printf("\nEnter the %d Values of Arrays", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
display(arr, n);
return 0;
}
int display(int arr[], int n)
{
int i;
printf("\nThe %d elements are:\n");
for (i = 0; i < n; i++)
{
printf("Array [%d]= %d\n", i, arr[i]);
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
I have written a C code to accept values via an array of structures which asks the user in the main function to choose whether he/she wants to input char or int values and sort the array via insertion sort and print it, but I am getting an
error from the compiler: {error]: expected expression before i
#include<stdio.h>
int n;
typedef struct ls{
int a;
char l;
}i[50],ls;
int main(){
printf("Enter the value\n");
scanf("%d",&n);
ls int1;
int ans;
printf("Main Menu\n");
printf("1.)integer sort \n2.)Characrter sort\n");
scanf("%d",&ans);
if(ans==1){
intSort(i.a);
}
else if(ans==2){
charSort(i.l);
}
else{
exit(0);
}
int ans1;
printf("Would you like to print the array\n");
scanf("%d",&ans1);
if(ans==1 && ans1==1){
display(i.a);
}
else{
display(i.l);
}
}
int intSort(int a[]){
int j;
printf("Enter the values into the array \n");
for(j=0;j<n;j++){
scanf("%d",&a[j]);
}
// insertion sort function call
insertionSort(a);
return 1;
}
int charSort(char a[]){
int j;
printf("Enter the values into the array \n");
for(j=0;j<n;j++){
scanf(" %c",&a[j]);
}
insertionSort(a);
return a;
}
int insertionSort(int a[]){
int temp,i,j;
for(i=1;i<n;i++){
temp= a[i];
j=i-1;
}
while(j>=0 && temp<a[j]){
a[j+1]=a[j];
j-=1;
}
a[j+1]=temp;
return 1;
}
int display(int a[]){
int i;
printf("Sorted array:");
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
}
You are defining the type incorrectly. If I understand correctly, you want do have a typedef ls and then initialize an array of your newly defined structures. You would want to do something like this:
#include <stdlib.h>
#include <stdio.h>
typedef struct ls {
int a;
char l;
} ls;
int main() {
int i;
ls *array_of_ls = malloc(50*sizeof(*array_of_ls));
for (i = 0; i < 50; i++) {
array_of_ls[i].a = i;
array_of_ls[i].l = 65+i;
printf("ls[%d] = (%d, %c)\n", i, array_of_ls[i].a, array_of_ls[i].l);
}
free(array_of_ls);
}
Of course, make sure you handle exceptions, etc. But this should get you going.
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 2 years ago.
Improve this question
At first code a[0] becomes 31 but in the second one a still 55. Can someone explain me the difference? What is the difference between changing a variable's and array's value in a function?
#include <stdio.h>
int h(int x[]);
int main()
{
printf("Hello, World!\n");
int a[0] = 55;
h(a);
printf("%d", a[0]);
return 0;
}
int h(int x[]) {
x[0] = 31;
}
#include <stdio.h>
int h(int x);
int main()
{
printf("Hello, World!\n");
int a= 55;
h(a);
printf("%d", a);
return 0;
}
int h(int x) {
x = 31;
}
And What about this code? This array is changed. I am really confused.
#include <stdio.h>
int h(int x[], int length);
int main()
{
int i;
int a[]= {5, 5, 5, 5};
h(a, 4);
printf("Array After Function Call\n");
for(i = 0; i < 4; i++) {
printf("%d ", a[i]);
}
return 0;
}
int h(int x[], int length) {
int i;
for(i = 0; i < length; i++) {
x[i] += x[i];
}
printf("Array in Function\n");
for(i = 0; i < length; i++) {
printf("%d ", x[i]);
}
printf("\n");
return 0;
}
And any decent book should have mentioned that arguments to functions are passed by value, meaning the value in the call is copied into the local function argument variable.
Modifying this local variable will only modify the local copy, not the value used in the call to the function.
One can overcome this problem by emulating pass by reference, which is done by passing a pointer to the variable:
void f(int *x)
{
*x = 123; // Dereference the pointer, to set the value of where it's pointing
}
int main(void)
{
int a = 0;
f(&a); // Pass a pointer to the variable a
}
This is really what's happening in the first program you show, you pass a pointer, and modify the value of where the pointer is pointing (the zero-sized array notwithstanding).
It seems that one of your major confusions is about your "array" argument.
When you declare a function as
void f(int a[]);
you don't actually declare an array argument: The compiler will treat it as a pointer and parse it as
void f(int *a);
Another point is that when you use array indexing, like
x[i] += x[i];
you are actually dereferencing the pointer, and write the values to the memory where the pointer is pointing. In fact, for any pointer or array x and index i, the expression x[i] is exactly equal to *(x + i).
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 6 years ago.
Improve this question
How to write a C Function that takes three integers as arguments and returns the value of the largest one.
int largest(int x,int y,int z)
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
printf("Largest number = %d", x);
if(y>=x && y>=z)
printf("Largest number = %d", y);
if(z>=x && z>=y)
printf("Largest number = %d", z);
}
I have tried this codes but they don't work i need help please I am also a beginner at this
This should work fine.
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z){
int max;
max=x;
if(y>max){
max=y;
}
if(z>max){
max=z;
}
return max;
}
Try this one:
#include <stdio.h>
int largest(int x, int y, int z);
int main() {
int val1, val2, val3;
int maximum;
printf("enter value \n");
scanf("%d", &val1, &val2, &val3);
maximum = largest(val1, val2, val3);
printf("the largest integer is %d = \n", maximum);
return 0;
}
int largest(int x, int y, int z){
if (x >= y && x >= z)
return x;
if (y >= x && y >= z)
return y;
// otherwise
return z;
}
The problem is that you wanted the method to return the largest value, but simply didnt do that - the code is not compiling because the largest function is defined to "return" an int but there's no return statement anywhere in your function.
If you dont know what exactly "returning function" is then take a look at this tutorial: http://www.cplusplus.com/doc/tutorial/functions/
This is a pretty short way of doing what you want:
int largest(int a, int b, int c)
{
a = (a > b) ? a : b;
a = (a > c) ? a : c;
return a;
}
To return a value you must use a return statement in function.
Let us inspect what you've done,
#include<stdio.h>
int largest(int x,int y,int z)/* missed ';' */
/* missed 'void main()' */
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d",&val1,&val2,&val3); /* missed format specifier for other two values */
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
printf("Largest number = %d", x);/* written print statement instead of return statement */
if(y>=x && y>=z)
printf("Largest number = %d", y);/* written print statement instead of return statement */
if(z>=x && z>=y)
printf("Largest number = %d", z);/* written print statement instead of return statement */
}
After modification the code should be like this,
#include<stdio.h>
int largest(int x,int y,int z);
int main()
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
return x;
if(y>=x && y>=z)
return y;
if(z>=x && z>=y)
return z;
}
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 have written the following program:
#include <stdio.h>
void printValue();
int main (){
int n = 100;
int i;
for (i=0; i<n; i+=1)
printValue();
}
void printValue(){
static unsigned int y = 0;
printf("y = %d", y);
y+=1;
}
How can I rewrite the algorithm to make it recursive?
#include <stdio.h>
void printValue(void);
void times(int n, void (*func)(void)){
if(n>0){
func();
times(--n, func);
}
}
int main (void){
int n = 100;
times(n, printValue);
return 0;
}
void printValue(void){
static unsigned int y = 0;
printf("y = %d\n", y);
y+=1;
}
#include <stdio.h>
void printValue(int);
void repeat_upto(int init_value, int end_value, int incremental,
void (*func)(int)){
if(incremental < 0 ? init_value >= end_value : init_value <= end_value){
func(init_value);
repeat_upto(init_value + incremental, end_value, incremental, func);
}
}
int main (void){
repeat_upto(0, 100-1, +1, printValue);
return 0;
}
void printValue(int v){
printf("%d\n", v);
}
#include <stdio.h>
void printValue(int v, int end_value){
if(v < end_value){
printf("%d\n", v);
printValue(v+1, end_value);
}
}
int main (void){
printValue(0, 100);
return 0;
}
This is almost the same as BLUEPIXY's answer since I believe it's the straight forward solution, but since you are confused by the function pointer, I removed that.
#include <stdio.h>
#include <stdlib.h>
void
printValue()
{
static unsigned int y;
printf("%d\n", y);
y += 1;
}
void
recursiveFunction(int counter)
{
printValue();
if (--counter == 0)
return;
recursiveFunction(counter);
}
int
main()
{
recursiveFunction(100);
return 0;
}
Or may be you mean this
#include <stdio.h>
#include <stdlib.h>
void
printValue(int y)
{
if (++y > 100)
return;
printf("%d\n", y);
printValue(y);
}
int
main()
{
printValue(0);
return 0;
}
Instead of
void printValue()
{
static unsigned int y = 0;
printf("y = %d", y);
y+1;
}
I turn it into:
void printValue(int y)
{
y++;
printf("y = %d\n", y);
printValue(y);
}
Compiled -> His function
Compiled -> Recursive function
See Same output, I just did what OP wanted.
Personal I would do this without recursive function avoid infinite loop:
for (i=0; i<n; i++)
{
printValue(y);
}
void printValue(int y){
printf("y = %d\n", y);
}