Thread: Program runs on mac but not on windows

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    3

    Program runs on mac but not on windows

    Code:
    #include <stdio.h>
    #include <string.h>
    void leftRotate(int a[], int t, int n);
    void leftRotatebyOne(int a[], int n);
    void printArray(int a[], int n);
    void rightRotatebyOne(int a[], int n);
    void rightRotate(int a[], int t, int n);
    
    
    int main(){
    int n;
    int i;
    char dir[1];
    int k;
    int t = 0;
    
    
    
    
    int a[1000];
    int new[1000];
    
    
    printf("Enter the amount of numbers in the array:\n");
    scanf("%d",&n);
    printf("This are the elements in your array:\n");
    for (i=0; i < n; i++)
    {
    	a[i]=(rand()%100);
    	printf("%d ",a[i]);
    }
    printf("\n");
    printf("Shift L or R?\n");
    scanf("%s",dir);
    printf("How many times?\n");
    scanf("%d",&t);
    
    
    if (*dir=='L')
    {
    	leftRotate(a, t, n);
    	printArray(a, n);
    }
    else if (*dir=='l')
    {
    	leftRotate(a, t, n);
    	printArray(a, n);
    }
    else if (*dir=='R')
    {
    	rightRotate(a, t, n);
    	printArray(a, n);
    }
    else
    {
    	rightRotate(a, t, n);
    	printArray(a, n);
    }
    
    
     return 0;
    }
    void leftRotate(int a[], int t, int n)
    {
      int i;
      for (i = 0; i < t; i++)
        leftRotatebyOne(a, n);
    }
    void leftRotatebyOne(int a[], int n)
    {
      int i, temp;
      temp = a[0];
      for (i = 0; i < n-1; i++)
         a[i] = a[i+1];
      a[i] = temp;
    }
    void printArray(int a[], int n)
    {
      int i;
      for(i = 0; i < n; i++)
        printf("%d ", a[i]);
    }
    
    
    void rightRotate(int a[], int t, int n)
    {
      int i;
      for (i = 0; i < t; i++)
        rightRotatebyOne(a, n);
    }
    void rightRotatebyOne(int a[], int n)
    {
      int i, temp;
      temp = a[n-1];
      for (i = (n-1); i > 0; i--)
         a[i] = a[i-1];
      a[i] = temp;
    }
    Runs and compiles on mac with no issues, but when comp and run it on windows it doesn't output the resultant array... Any help would be appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    > char dir[1];
    You need at least 2 chars to store the result of a scanf("%s")
    Otherwise you have a buffer overflow.

    If you really want just one char, then this might be better for reading the first non-space character.
    char dir;
    scanf(" %c",&dir); // note the leading space
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    3
    Thank you salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile/ runs on MAC but not on Windows?
    By SlowSpeedChase in forum C Programming
    Replies: 3
    Last Post: 12-02-2013, 06:18 PM
  2. Code runs on Windows 7 but not Vista?
    By gken05 in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2011, 12:51 PM
  3. Replies: 6
    Last Post: 09-18-2010, 08:45 PM
  4. Runs in Windows but not un Linux (Ubuntu)
    By Joelito in forum C++ Programming
    Replies: 5
    Last Post: 07-29-2009, 06:49 PM
  5. windows installer for psp runs for vs6
    By iain in forum Tech Board
    Replies: 1
    Last Post: 06-27-2004, 09:21 AM

Tags for this Thread