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.
Related
Basically this problem asks me to get an input of target_sum and check the integers in the array. If a sum of a pair is equal to target_sum, I need to print it.
I wrote the code like this but it does not work. Where did I make the mistake?
void findPair (int arr[],int size,int target_sum)
{
int i,j;
for (i=0;i<size;i++){
for (j=0;j<size;j++){
if (arr[i] + arr[j] == target_sum)
{
printf("(%d,%d)",arr[i],arr[j]);
}
else
{
printf("No pair.");
}
}
}
}
int main(){
int arr[] = {8,7,2,5,3,1};
int target_sum;
printf("Enter target sum: ");
scanf("%d",target_sum);
findPair(arr,6,target_sum);
}
As #nsilent22 suggested, scanf("%d",target_sum); should be changed into scanf("%d",&target_sum);. Besides that, you could have the second for loop start at j=i+1, making your code overall look like this:
#include <stdio.h>
void findPair (int arr[],int size,int target_sum)
{
int i,j;
for (i=0;i<size;i++)
for (j=i+1;j<size;j++)
if (arr[i] + arr[j] == target_sum)
printf("\n(%d,%d)",arr[i],arr[j]);
else
printf("\nNo pair.");
}
int main(){
int arr[] = {8,7,2,5,3,1};
int target_sum;
printf("Enter target sum: ");
scanf("%d",&target_sum);
findPair(arr,6,target_sum);
}
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;
}
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 wrote the following code to find the max number from a array of numbers. Apparently there is an error in my code.It is a segmentation error. Please help me identify it.
#include <stdio.h>
void max(int n,int A[n]);
int main()
{
int n;
int A[n];
max(n,A[n]);
}
void max(int n,int A[n])
{
printf("Enter the number of elements you want in your array\n");
scanf("%d",&n);
int i;
printf("Enter the elements in your array\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
int max=A[0];
for(i=1;i<n;i++)
{
if(A[i]>max)
{
max=A[i];
}
}
printf("%d",max);
}
int n;
int A[n];
You have to initialize n otherwise it contains garbage value. And now after this point you run into undefined behavior.
Correct code would be
#include <stdio.h>
#include <stdlib.h>
void printMax(int n,int A[]);
int main()
{
size_t n;
printf("Enter the number of elements you want in your array\n");
if( scanf("%zu",&n) != 1){
fprintf(stderr,"Error in input");
}
if( n <= 0){
fprintf(stderr, "%s\n", "Error in input : Enter number >= 0 .");
}
int a[n];
printf("Enter the elements in your array\n");
for(size_t i = 0; i < n; i++)
{
if( scanf("%d",&a[i]) != 1){
fprintf(stderr,"%s\n","Error in input");
exit(1);
}
}
printMax(n,a);
return 0;
}
void printMax(size_t n,int A[])
{
int max=A[0];
for(size_t i = 1; i < n; i++)
if(A[i] > max)
max = A[i];
printf("%d",max);
}
In main() you declare n but it has no value, so likely defaults to 0. You then declare and define an array A and give it size n, which as I say is likely zero.
Within max() you then read in a value and assign it to n but your array A is size zero.
So change main() to
/* Get the number of items to store in the array */
int n;
printf("Enter the number of elements you want in your array\n");
scanf("%d",&n);
/* Create the array of the given size */
int A[n];
/* Now find the max value in that array */
max(n,A);
And remove the setting of n from max().
I'm writing a code for my C programming class and stumbled upon a problem. I'm supposed to write a program which will show as an output Pascal's triangle. I'm to use 1d arrays and in each iteration make the array bigger by using realloc. The trouble is that even though the code compiles and runs when I type eg '7' (as the height of the tringle) in the 7th column there will be trash number. I have no idea why it happens. I'm a beginner in dynamic memory allocation, so please by gentle.
Here's my code:
int i,n;
printf("Give the height:\n");
scanf("%d", &n);
int *tab = (int*)malloc(sizeof(int));
int *tab2, liczba=2;
for(i=0;i<n;i++)
{
tab2=(int *)realloc(tab,i+1);
tab2[i]=&liczba;
print(tab2, i+1);
printf("\n");
}
void print(int *tab, int size)
{
int i;
for(i=0;i<size;i++) printf("%d\t", tab[i]);
}
#include <stdio.h>
#include <stdlib.h>
void print(int *tab, int size);
int main(void){
int i, j, n;
printf("Give the height:\n");
scanf("%d", &n);
int *tab = NULL;
for(i=1;i<=n;++i){
int *temp = realloc(tab, i * sizeof(*tab));
if(temp)
tab = temp;
else {
perror("realloc");
free(tab);
exit(EXIT_FAILURE);
}
tab[i-1] = (i == 1) ? 1 : 0;
for(j=i-1;j>0;--j){
tab[j] += tab[j-1];
}
print(tab, i);
}
free(tab);
return 0;
}
void print(int *tab, int size){
int i;
for(i=0;i<size;i++){
if(i)
putchar('\t');
printf("%d", tab[i]);
}
putchar('\n');
}
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 8 years ago.
Improve this question
I have written a code and it doesn't run. However, there are no mistakes in compiler. I am stuck, could you give me a hint please where am I wrong?
I am 95% sure it is about checktable function.
P.S. I know how to do it with pointers, but I try to understand how to make it without them.
Thank you!
#include <stdio.h>
#include "genlib.h"
void fillarray (int a[50])
{
int i;
for(i=0;i<50;i++)
a[i]=rand()%10;
}
void printarray (int a[50])
{
int i;
for(i=0;i<50;i++)
printf(" %d ", a[i]);
}
int number()
{
int num;
printf("Give the number!");
num=GetInteger();
return num;
}
void checktable (int a[50], int b[50], int ar,int count)
{
int i;
count=0;
for(i=0;i<50;i++)
if (a[i]==ar)
{
b[count]=i;
count++;
}
}
main()
{
int i,a[50], b[50], N,count;
fillarray(a);
printarray(a);
N=number();
checktable(a,b,N,count);
printf("the number is used %d times", count);
printf("in places:");
for(i=0;i<count;i++)
printf("%d ",b[i]);
getch();
}
Approach 1: The "pointer" way:
In your code, change
void checktable (int a[50], int b[50], int ar,int count)
to
void checktable (int a[50], int b[50], int ar,int *count)
and call it as checktable(a,b,N,&count);
Approach 2: The "return" way:
Otherwise, the count which is updated in checktable() won't be reflected in main().
However, Alternatively (without using pointers), IMO, its always easier to simply return the number of occurrences via return statement. In that case , code will look like
int checktable (int a[50], int b[50], int ar)
{
int i;
int count=0;
for(i=0;i<50;i++)
if (a[i]==ar)
{
b[count]=i;
count++;
}
return count;
}
and the calling will be
count = checktable(a,b,N);
Note: Its always a good practice to initialize your variables.
If you don't want to use pointers,then you can return count and assign it to the count variable in main. I've improved your code here with all of it explained in the comments inside the code:
#include <stdio.h>
#include <stdlib.h> //Don't forget this header for srand and rand
#include <time.h> //For the time in srand
#include <conio.h> //For using getch()
#include "genlib.h"
void fillarray (int a[]) //leave the brackets empty
{
int i;
for(i=0;i<50;i++)
a[i]=rand()%10;
}
void printarray (int a[]) //here too
{
int i;
for(i=0;i<50;i++)
printf(" %d ", a[i]);
}
int number()
{
int num;
printf("\n\nGive the number!");
num=GetInteger();
return num;
}
int checktable (int a[], int b[], int ar) //Use int to return an int
//No need for count parameter
{
int i,count;
count=0;
for(i=0;i<50;i++)
if (a[i]==ar)
{
b[count]=i;
count++;
}
return count; //returning count
}
int main() //Use int main
{
int i,a[50], b[50], N,count;
srand(time(NULL)); //For rand to generate random numbers
fillarray(a);
printarray(a);
N=number();
count=checktable(a,b,N); //set count to the return value of checktable
//Also,no need to pass count as a parameter
printf("The number is used %d times", count);
printf(" in places:");
for(i=0;i<count;i++)
printf("%d ",b[i]+1); //Don't forget +1 otherwise output might come as place 0
getch();
return 0; //main returns int
}