This is my code. main should have only calling function. testcases() call the test cases run through the program.
#include<stdio.h>
#include"conio.h"
int main()
{
testcases();
}
struct test {
int a[10];
} testDB[5] = {
{1,2,3,4,5,6},
{7,8,9,0,1,2,3,4}
};
void testcases()
{
int i;
for(i=0;i<2;i++)
displaytest(testDB[i].a);
}
displaytest(char *a)
{
int i=0;
while(a[i]!='\0')
{
printf("%d\n",a[i]);
i++;
}
}
I want to display both the arrays. But I am getting only first indexes.
Can anyone help ?
Your passed parameter is not appropriate;
displaytest(char *a) --> void displaytest(int *a)
EDIT: Your while loop won't work, as stated in the first comment.
You can not check like this while(a[i]!='\0')
You have to pass the size of the array as 2nd parameter.
void displaytest(int *a, int size)
{
int i = 0;
while (i < size/sizeof(int))
{
printf("%d\n", a[i]);
i++;
}
}
While calling, you can call like this...
displaytest(testDB[i].a, sizeof(testDB[i].a));
Related
The code is running fine on my Dev C++ IDE, but it keeps failing on the HackerRank IDE.
Am I making some mistake?
Here's a link to the problem:
https://www.hackerrank.com/challenges/arrays-ds/problem?isFullScreen=true
The function I wrote is as follows, please explain to me where am I going wrong?
int* reverseArray(int a_count, int* a, int* result_count) {
int i, j;
for(i=0; i<a_count; i++) {
result_count[i] = a[i];
}
for(j=a_count-1; j>=0; j--) {
printf("%d ",result_count[j]);
}
return result_count;
}
Your answer is corrct ,you copy the elements of array 1 from end to start in array 2
if you want to reverse your array in the same array,you just use variable temporary to swap the elements like this :
#include<stdio.h>
void* reverseArray(int a_count, int* a) //you return an array (type complexe) so ,you should use void
{
int temp=0;
for(int i=0; i<a_count/2; i++)
{
temp= a[a_count-i-1];
a[a_count-i-1]=a[i];
a[i]=temp;
}
for(int i=0; i<a_count; i++)
{
printf("%d ",a[i]);
}
}
int main()
{
int t[5]={1,2,3,4,5};
reverseArray(5,t);
}
#include<stdio.h>
struct Ques
{
int a;
}Q[5];
void sort(int a[])
{
printf("any sort technique...");
}
void main()
{
sort(Q.a);
}
So this is the sample code.
I Want to access the whole struct element as array.
You want this:
#include<stdio.h>
struct Ques
{
int a;
} Q[5];
void sort(struct Ques array[], int size)
{
printf("any sort technique...");
// just some demo
for (int i = 0; i < size; i++)
printf("array[%d].a = %d\n", i, array[i].a);
}
int main()
{
// put some data into Q
sort(Q, 5);
}
sort needs two parameters:
the pointer to the array to sort
the size of the array (unless you only ever want to sort array of some fixed size
im trying to pass a 2D array from main to a function and trying to print it letter by letter
but it keeps giving me segmentation fault
note: the question im trying to solve as mentioned a function with parameter { ArrPrintMatrix(char *(p)[7]) }
so help me by keeping the above thing in mind
#include<stdio.h>
ArrPrintMatrix(char **p,int n) {
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<10;j++) {
printf("%c ",p[i][j]);
}
}
}
main() {
int i;
char c[2][10];
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix((char **) c,2);
}
You should use char p[2][10] not char** p
The following code could work:
#include <stdio.h>
void ArrPrintMatrix(char p[2][10], int n) {
int i;
for (i = 0; i < n; ++i)
printf("%s\n",p[i]);
}
int main() {
int i;
char c[2][10];
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix(c,2);
return 0;
}
you need to change the type of the p var in the print function, and you should also set the array to zero so if the strings that are printing are less than 10 chars with terminator- garbage values are not displayed.
void ArrPrintMatrix(char p[][10],int n) {
int i,j;
for(i=0;i<n;i++) {
for(j=0;j<10;j++) {
printf("%c ",p[i][j]);
}
}
}
int main() {
int i;
char c[2][10]= {0};
puts("enter two strings");
for(i=0;i<2;i++)
scanf("%s",c[i]);
ArrPrintMatrix( c,2);
return 0;
}
I'm trying to make function that prints array but the output of it is wrong.
Can someone help me please?
This is the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void printArr(int arr[],int size);
int main()
{
int arr1[3] = { 1,2,3 };
int arr2[5] = { 1,2,3,4,5 };
printf("arr1: \n");
printArr(arr1, 3);
printf("\n\narr2: \n");
printArr(arr2, 5);
printf("\n\n");
return(0);
}
void printArr(int arr[], int size)
{
int i;
for (i = 0; i < size; i++);
{
printf("%d", arr[i]);
}
}
What I get is:
Remove the semi-colon at the of for:
for (i = 0; i < size; i++);
^^^
That makes the for loop run size times and executes the block after that. But by that time, i value is equal to size. This leads to out of bound access, which is undefined behaviour. Clearly, this is not what you intention was.
I'm trying to print array of pointer using pointer instead of array but I got this error Segmentation fault at runtime:
enter number of element:5
array[0]=1
array[1]=2
array[2]=3
array[3]=4
array[4]=5
Segmentation fault
This is the code:
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int *array,int n);
void display(int *array,int n);
int sum(int *array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(array,n);
display(array,n);
result=sum(array,n);
printf("sum of array=%d",result);
return 0;
}
void input(int *array,int n){
int j;
array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",array+j);
}
}
void display(int *array,int n){
int j;
for(j=0;j<n;j++)
printf("%d\t",*(array+j));
printf("\n");
}
int sum(int *array,int n){
int sum=0,j;
for(j=0;j<n;j++)
sum+=*array+j;
return sum;
}
How can I fixed this code? please somebody explain me what's wrong with that code.
Variable array is a local variable in function input.
As such, it is pointless to set it with array = ..., because this assignment takes effect only inside the function. You should typically pass its address (&array) to any function that needs to change it.
In your specific example, you also have a global variable array, so a quick solution to your problem would be to simply call function input without passing variable array as an argument:
void input(int n)
{
...
array = (int*)malloc(n*sizeof(int));
...
}
int main()
{
...
input(n);
...
}
Note that this is a "dirty" workaround, and you should typically strive to avoid the use of global variables.
To add the clean version to barak's answer:
int input(int ** array, const size_t n)
{
int result = 0;
assert(NULL != array);
(*array) = malloc(n * sizeof(**array));
if (NULL == (*array))
{
result = -1;
}
else
{
size_t j;
for(j = 0; j < n; ++j)
{
printf("array[%zu]=", j);
scanf("%d", (*array) + j); /* still missing error checking here . */
}
}
return result;
}
And call it like this:
if (-1 == input(&array, n))
{
perror("input() failed");
exit(EXIT_FAILURE);
}
Try this input():
void input(int **array,int n){
int j;
*array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);scanf("%d",*array+j);
}
}
Because C use pass-by-value, if you want to change the value of a variable in a function, you need to pass the address of that variable as the argument to that function.
In this case, you want to change the value of array in input() and the type of array is int *, therefore the prototype of input() should be something like void input (int **array, ...).
this should do..make sure you understand what the others have said..
#include <stdio.h>
#include <stdlib.h>
int *array;
int n;
void input(int **array,int n);
void display(int **array,int n);
int sum(int **array,int n);
int main (void) {
int result;
printf("enter number of element:");scanf("%d",&n);
input(&array,n);
display(&array,n);
result = sum(&array,n);
printf("sum of array= %d",result);
return 0;
}
void input(int **array,int n){
int j;
*array= malloc(n*sizeof(int));
for(j=0;j<n;j++){
printf("array[%d]=",j);
scanf("%d",(*array)+j);
}
}
void display(int **array,int n){
int j;
for(j=0;j<n;j++){
printf("%d\t",*((*array)+j)); // you can use array notation aswell
//array[0][j] will work
}
printf("\n");
}
int sum(int **array,int n){
int sum=0,j;
for(j=0;j<n;j++){
sum += *((*array)+j);
}
return sum;
}
What does *array + j do? Does it evaluate *array and add j to it? Or does it add j to array and then dereference it? Would you be willing to bet $100 on it if I told you you are wrong?
Make your life and the life of anybody reading your code easier by using parentheses, or even better, write array [j].